-5

我有一个字符串,包括 -

lblWithText.backgroundColor = [UIColor clearColor];
    lblWithText.textColor = [UIColor blackColor];
    lblWithText.font = [UIFont fontWithName:@"Helvetica" size:12.0];
    lblWithText.text = [NSString stringWithFormat:@"Blah1:blah-blah%d. Blah2:-%d%%", [currentCoupon.couponPrice intValue],[currentCoupon.couponDiscountPercent intValue]];

请告诉我该怎么做“Blah2:-%d%%”是Bolden?在此先感谢

4

4 回答 4

6

Probably this can help you

Bold & Non-Bold Text In A Single UILabel?

it has solution for iOS 6 and for iOS5 you will have to use a CATextLayer with an NSAttributedString

or you may refer to http://www.cocoacontrols.com/platforms/ios/controls/ohattributedlabel

https://github.com/AliSoftware/OHAttributedLabel/

https://github.com/mattt/TTTAttributedLabel/

于 2012-12-28T11:56:54.147 回答
4

只需要Helvetica带有粗体的字体名称

lblWithText.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];

只是一个例子..

UILable *lbl1 = [[UILable alloc]init];
lbl1.frame = CGRectMake(10,10,100,40);
lbl1.backgroundColor = [UIColor clearColor];
lbl1.textColor = [UIColor blackColor];
lbl1.font = [UIFont fontWithName:@"Helvetica" size:12.0];
lbl1.text = @"Stack";

UILable *lbl2 = [[UILable alloc]init];
lbl2.frame = CGRectMake(110,10,150,40);
lbl2.backgroundColor = [UIColor clearColor];
lbl2.textColor = [UIColor blackColor];
lbl2.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0];
lbl2.text = @"OverFlow";

[self.view addSubview:lbl1];
[self.view addSubview:lbl2];

这是一个通过你得到想法的例子..

更新:

另请参见使用 NSMutableAttributedString 的示例:

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Blah1:blah-blah%d. Blah2:-%d%%", [currentCoupon.couponPrice intValue],[currentCoupon.couponDiscountPercent intValue]];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:NSMakeRange(0,30)];/// Define Range here and also BackGround color which you want
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,30)];/// Define Range here and also TextColor color which you want
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
lblWithText.attributedText = str;
于 2012-12-28T11:42:41.987 回答
1

对于全粗体,你可以试试这个

[lblWithText setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12.f]]

也可以去这个链接查看各种字体

正如您所说,您希望某些部分像正常的粗体一样,然后您可以尝试属性字符串,但不确定。

或者去这个链接这个

于 2012-12-28T11:51:58.683 回答
1

为此请使用 NSMutableAttributedString 请按照此教程进行 NSMutableAttributedString

http://soulwithmobiletechnology.blogspot.in/2012/07/how-to-use-nsattributedstring-in-ios-6.html

于 2012-12-28T11:56:27.740 回答