1. 程式人生 > >iOS改變Label上指定文字的顏色

iOS改變Label上指定文字的顏色

    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 20)];
    textLabel.text = @"你好啊世界";
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:label.text]; // 改變特定範圍顏色大小要用的
    NSRange *r = NSMakeRange(1, 1);
    NSRange *r2 = NSMakeRange(2, 2);

    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:r];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:r2]
    [label setAttributedText:attributedString];
    // 這樣"好"是紅色的"啊世"是綠色的

相關推薦

iOS改變Label指定文字顏色

UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 100, 20)]; textLabel

[紹棠] iOS設定Label顯示不同字型大小和字型顏色

一, 一個label上顯示不同的字型大小 NSString *needText = @"個人訊息(11)";                 [topLabel setAttributedTe

iOS設定指定文字顏色為紅色

//設定 指定內容顯示成紅色NSString *[email protected]"閱讀並接受《風險披露》宣告";NSMutableAttributedString *att = [[NSMutableAttributedString alloc]initWith

點按UITableViewCell時改變cell面的文字高亮或者背景顏色

點按某個Cell的時候cell上面的文字高亮或者改變顏色在具體的Cell中重寫函式: - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { //重寫高亮函式 if (highlighted) {

SecureCR 改變背景色和文字顏色

global ESS img bold linu 文件 info 快捷 就是 1.打開SecureCR鏈接Linux服務器,Options->Session Options->Emulation->Terminal 選擇Linux (相應的服務器系統)AN

iOS在image文字-畫圖片

專案想設定在圖片上新增圓形圖片,可獲取不到imageView,實在沒辦法就在圖片上畫上圖片 Demo: // 在圖片上新增圖片;imageName 1.底部圖片名字imageName, image2 需新增的圖片 - (UIImage *)createShareImage:(UIIma

ios :一個Label 顯示兩種顏色的寫法

NSMutableAttributedString *commentAttrString = [[NSMutableAttributedString alloc] initWithString:@" 條評論" attributes:@{NSFontAttribu

iOS UITabBarController tabbar 面的文字title 怎麼移動位置

/* To set item label text attributes use the appearance selectors available on the superclass, UIBarItem.    Use the following to tweak the relative posit

ios 設定label 不同字型大小顏色

建立分類    #import"UILabel+AutoSet.h"    此分類目的是實現對一個UILabel中的字型大小顏色進行改變。使用方法,在使用的地方引用標頭檔案 #import "UILabel+AutoSet.h"    或者 直接放到PrefixHeader檔

IOS改變TABBAR高度、背景顏色

自定義MyTabBarController繼承UITabBarController 重寫方法 其中TABBAR_HEIGHT為自定義的高度 -(void)viewWillLayoutSubviews{ CGRecttabFrame=self.tabBar.frame; t

改變FragmentTabHost選中的文字顏色

使用FragmentTabHost時遇到選中某一個Tab後字型顏色不改變的問題,可以使用以下方法來解決: mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { @

滑鼠經過超連結,元素(li)背景變色的程式碼,同時文字顏色改變

效果如下圖: 要實現這個效果。很簡單。 定義CSS樣式: a:hover{    background:#f29901;} 這段程式碼的顯示效果如下圖 顯然,這樣太醜了。 只需要再加上一句 a:hover{     bac

改變UIButton標題位置、文字顏色、背景色等

示例程式碼+註釋 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, 200, 100)]; //新增點選事件 [button addTarget:se

iOS文字label字型大小和顏色

實現:最初實現的時候想到了用兩個Label,來實現,第一個顯示¥4000,設定一個字型,第二個顯示/月,設定另一個字型.這樣就能實現這個效果了,但是最後想一想還是用富文字比較好,順便可以學習一下. //先建立一個label: -(UILabel *)pri

iOS」怎麼修改去掉Navigation Bar的返回按鈕文字顏色,箭頭顏色以及導航欄按鈕的顏色

解決方法 1: 自從IOS7後UINavigationBar的一些屬性的行為發生了變化.你可以在下圖看到: 背景顏色 self.navigationController.n

表單輸入框中如何改變光標的顏色文字顏色不變

效果 caret 如何改變 info 紅色 圖片 css 分享 mage 如圖所示,要想改變光標顏色,可以通過css的caret-color來設置 input { caret-color: red; } 顯示效果,鼠標點擊輸入框之後,光標顏色

iOS for循環創建button,button的寬度依據面的文字來自適應.

event int system nil draw cgrect fragment ng- 計算 近期須要使用一個標簽頁,尋思自己寫一個demo. 標簽的大小依據

iOS StatusBar狀態欄文字顏色更改

sel tro 所有 ani 更新 statusbar isa rst ear 1、將單個頁面的StatusBar文字顏色改為白色,動態更改也可以在這裏設置 -(UIStatusBarStyle)preferredStatusBarStyle { return U

小程式切換tab時tab文字顏色不正常問題

問題原因:小程式json中tab的顏色只支援二進位制 程式碼如下: 關鍵程式碼為; "backgroundColor": "#ffffff", "color": "#000000", "selectedColor": "#C62724"

MFC中一種改變靜態文字顏色的方法

步驟如下: 1、新增全域性變數:       在.cpp檔案中新增全域性變數宣告:       BYTE R,G,B; 2、手動新增OnCtlColor()函式  &nb