The Colorful UILabel in iOS development

# The Colorful UILabel in iOS development

---

Once up on a time,I had a request which needed to display a colorful UILabel in the screen.

The product manager said the colorful texts can catch the eyes of audiences.

OK,I sure chose to believe them.

Then,I had a question:

## 1. How to set different colors to each word in the UILabel?

---

I started my work with my own ideas:

- An UILabel can display the texts;

- The color of the texts can be set by 'UILabel.textColor';

- Adding two UILabels with different colors will do;

- Excellent! (I said to myself).

I opened the Xcode.app to edit my codes like this:

### 1.1 The way I did in the past time

```

/**

Initializes a colorful label container view

- returns: A colorful label container view instance

*/

func initColorfulLabel() -> UIView {

let containerViewFrame:CGRect = CGRect(x: -7, y: 100, width: self.view.bounds.width, height: 20);

let containerView:UIView = UIView(frame: containerViewFrame);

let redLabelFrame = CGRect(x: 0, y: 0, width: containerView.bounds.width / 2, height: 20);

let redLabel:UILabel = UILabel(frame: redLabelFrame);

redLabel.text = "RED";

redLabel.textColor = UIColor.redColor();

redLabel.textAlignment = NSTextAlignment.Right;

containerView.addSubview(redLabel);

let blueLabelFrame = CGRect(x: containerView.bounds.width / 2, y: 0, width: containerView.bounds.width / 2, height: 20);

let blueLabel:UILabel = UILabel(frame: blueLabelFrame);

blueLabel.text = " BLUE";

blueLabel.textColor = UIColor.blueColor();

blueLabel.textAlignment = NSTextAlignment.Left;

containerView.addSubview(blueLabel);

return containerView;

}

```

Oh,My God,It worked! ^0^:

![colorfulLabelsInContainerView.png](/images/colorful-uilabel/colorfulLabelsInContainerView.png)

### 1.2 The suggested way a mentor told me recently

My mentor told me that UILabel supports different texts with different colors by setting UILabel's attributedText.

Here is the newer one:

```

/**

Initializes a real colorful UILabel instance instead of a container view,this method is guided by a mentor

- returns: A real colorful UILabel instance

*/

func initColorfulLabelOfMentor() -> UILabel {

let str:NSMutableAttributedString = NSMutableAttributedString(string: "RED BLUE");

str.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSMakeRange(0, 3));

str.addAttribute(NSForegroundColorAttributeName, value: UIColor.blueColor(), range: NSMakeRange(4, 4));

let labelFrame = CGRect(x: 0, y: 140, width: self.view.bounds.width, height: 20);

let label:UILabel = UILabel(frame: labelFrame);

label.attributedText = str;

label.textAlignment = NSTextAlignment.Center;

return label;

}

```

It works too:

![colorfulLabel.png](/images/colorful-uilabel/colorfulLabel.png)

After I made the colorful texts in the screen,the product manager said we still need to add a shadow to the text, and again, for catching the eyes of audiences.

OK,I would do it. Because I always chose to believe them.

So,I had a new question here:

## 2. How to add a shadow to the words in UILabel?

---

I started my thinking again,of course I chose to add two different UILabels to make the shadow:

- Add an UILabel to display the given texts with certain color(color of the shadow);

- Add another UILabel to display the texts with shadow color;

- Put the two UILabels together into a container UIView to make a shadow;

- Return the UIView instance called 'containerView'.

So,I put my ideas into codes:

### 2.1 The way I did in the past time

Here is the implementation of my idea:

```

/**

Initializes a shadow label container view

- returns: A UIView instance which contains two UILabels to display a shadow

*/

func initShadowLabel() -> UIView {

let containerViewFrame:CGRect = CGRect(x: 0, y: 180, width: self.view.bounds.width, height: 20);

let containerView:UIView = UIView(frame: containerViewFrame);

let lowerLabelFrame = CGRect(x: 1, y: 1, width: self.view.bounds.width, height: 20);

let lowerLabel:UILabel = UILabel(frame: lowerLabelFrame);

lowerLabel.text = "LABEL WITH SHADOW";

lowerLabel.textColor = UIColor.blueColor();

lowerLabel.textAlignment = NSTextAlignment.Center;

containerView.addSubview(lowerLabel);

let upperLabelFrame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 20);

let upperLabel:UILabel = UILabel(frame: upperLabelFrame);

upperLabel.text = "LABEL WITH SHADOW";

upperLabel.textColor = UIColor.redColor();

upperLabel.textAlignment = NSTextAlignment.Center;

containerView.addSubview(upperLabel);

return containerView;

}

```

Oh,My God,It worked again:

![shadowLabelsInContainerView.png](/images/colorful-uilabel/shadowLabelsInContainerView.png)

### 2.2 The suggested way of a mentor

Once again,a mentor occured,And he gave me another way to implement the shadow in UILabel:

```

/**

Initializes a real shadow UILabel instance instead of a container view,this method is guided by a mentor

- returns: A UILabel which has a shadow

*/

func initShadowLabelOfMentor() -> UILabel {

let labelFrame = CGRect(x: 0, y: 220, width: self.view.bounds.width, height: 20);

let label:UILabel = UILabel(frame: labelFrame);

label.text = "LABEL WITH SHADOW";

label.textColor = UIColor.redColor();

label.shadowColor = UIColor.blueColor();

label.shadowOffset = CGSize(width: 1, height: 1);

label.textAlignment = NSTextAlignment.Center;

return label;

}

```

See? It is more simple and clearer.

Here is the result in picture:

![shadowLabel.png](/images/colorful-uilabel/shadowLabel.png)

## 3. Source Code:

You can find all the codes [Here](https://github.com/acttos/ColorfulLabel){:target="_blank"}.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末申眼,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子馏慨,更是在濱河造成了極大的恐慌,老刑警劉巖荐吵,帶你破解...
    沈念sama閱讀 217,826評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件锅棕,死亡現(xiàn)場離奇詭異巡语,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)分别,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門遍愿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人耘斩,你說我怎么就攤上這事沼填。” “怎么了括授?”我有些...
    開封第一講書人閱讀 164,234評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵坞笙,是天一觀的道長岩饼。 經(jīng)常有香客問我,道長薛夜,這世上最難降的妖魔是什么籍茧? 我笑而不...
    開封第一講書人閱讀 58,562評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮梯澜,結(jié)果婚禮上寞冯,老公的妹妹穿的比我還像新娘。我一直安慰自己晚伙,他們只是感情好吮龄,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,611評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著咆疗,像睡著了一般漓帚。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上午磁,一...
    開封第一講書人閱讀 51,482評(píng)論 1 302
  • 那天尝抖,我揣著相機(jī)與錄音,去河邊找鬼迅皇。 笑死昧辽,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的喧半。 我是一名探鬼主播奴迅,決...
    沈念sama閱讀 40,271評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼青责,長吁一口氣:“原來是場噩夢啊……” “哼挺据!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起脖隶,我...
    開封第一講書人閱讀 39,166評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤扁耐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后产阱,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體婉称,經(jīng)...
    沈念sama閱讀 45,608評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,814評(píng)論 3 336
  • 正文 我和宋清朗相戀三年构蹬,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了王暗。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,926評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡庄敛,死狀恐怖俗壹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情藻烤,我是刑警寧澤绷雏,帶...
    沈念sama閱讀 35,644評(píng)論 5 346
  • 正文 年R本政府宣布头滔,位于F島的核電站,受9級(jí)特大地震影響涎显,放射性物質(zhì)發(fā)生泄漏坤检。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,249評(píng)論 3 329
  • 文/蒙蒙 一期吓、第九天 我趴在偏房一處隱蔽的房頂上張望早歇。 院中可真熱鬧,春花似錦讨勤、人聲如沸缺前。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽衅码。三九已至,卻和暖如春脊岳,著一層夾襖步出監(jiān)牢的瞬間逝段,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評(píng)論 1 269
  • 我被黑心中介騙來泰國打工割捅, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奶躯,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,063評(píng)論 3 370
  • 正文 我出身青樓亿驾,卻偏偏與公主長得像嘹黔,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子莫瞬,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,871評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • The Colorful UILabel in iOS development Once up on a time...
    HuluCat521閱讀 273評(píng)論 0 2
  • 個(gè)人成長的一個(gè)重要方面就是對(duì)自己的性生活進(jìn)行反思儡蔓。性可以是一種正能量,也可以是一種負(fù)能量疼邀,這取決于它的表現(xiàn)方式喂江。作...
    申公鮑閱讀 1,571評(píng)論 0 0
  • 有的夢很嚇人 但是最終都會(huì)醒來 現(xiàn)實(shí)有時(shí)候很幸福 失去了卻再也找不回 有的夢很甜 醒來了還可以一直回味 現(xiàn)實(shí)的幸福...
    Maxine9閱讀 166評(píng)論 0 0
  • 和國內(nèi)的自由行很不同的是获询,國內(nèi)想到景點(diǎn)排隊(duì),吃飯排隊(duì)拐袜,買票排隊(duì)吉嚣,住宿排隊(duì),內(nèi)心都會(huì)塞滿絕望蹬铺,到了地廣人稀的地方尝哆,突...
    河囚閱讀 2,069評(píng)論 0 6
  • 表達(dá)生活的愜意,不過是養(yǎng)花養(yǎng)草養(yǎng)金魚丛塌,讀書喝茶敷黃瓜较解。
    南溪有佳人閱讀 162評(píng)論 0 2