我宁愿不使用UIWebVIEw ..我也读过这可能是可能使用NSAttributedString,但我不知道如何使用它。有任何想法吗?
苹果在几个应用程序中实现了这一点;
示例屏幕截图:
谢谢!
– Dom
在Swift中,我们不必处理iOS5旧的东西,除了语法更短,所以一切变得非常简单:
func attributedString(from string: String,nonBoldRange: NSRange?) -> NSAttributedString { let FontSize = UIFont.systemFontSize let attrs = [ NSFontAttributename: UIFont.boldSystemFont(ofSize: FontSize),NSForegroundcolorAttributename: UIcolor.black ] let nonBoldAttribute = [ NSFontAttributename: UIFont.systemFont(ofSize: FontSize),] let attrStr = NSMutableAttributedString(string: string,attributes: attrs) if let range = nonBoldRange { attrStr.setAttributes(nonBoldAttribute,range: range) } return attrStr}
用法:
let targetString = "Updated 2012/10/14 21:59 PM"let range = NSMakeRange(7,12)let label = UILabel(frame: CGRect(x:0,y:0,wIDth:350,height:44))label.backgroundcolor = UIcolor.whitelabel.attributedText = attributedString(from: targetString,nonBoldRange: range)label.sizetoFit()
奖金:国际化
有些人评论国际化。我个人认为这是超出这个问题的范围,但对于教学目的,这是我会怎么做
// Date we want to showlet date = Date()// Create the string.// I don't set the locale because the default locale of the formatter is `NSLocale.current` so it's good for internationalisation :plet formatter = DateFormatter()formatter.dateStyle = .mediumformatter.timeStyle = .shortlet targetString = String(format: NSLocalizedString("Update %@",comment: "Updated string format"),formatter.string(from: date))// Find the range of the non-bold partformatter.timeStyle = .nonelet nonBoldRange = targetString.range(of: formatter.string(from: date))// Convert Range<Int> into NSRangelet nonBoldNSRange: NSRange? = nonBoldRange == nil ? nil : NSMakeRange(targetString.distance(from: targetString.startIndex,to: nonBoldRange!.lowerBound),targetString.distance(from: nonBoldRange!.lowerBound,to: nonBoldRange!.upperBound))// Now just build the attributed string as before :)label.attributedText = attributedString(from: targetString,nonBoldRange: nonBoldNSRange)
结果(假设英语和日语Localizable.strings可用)
iOS6和更高版本的上一个答案(Objective-C仍然有效):
在iOS6 UILabel,UIbutton,UITextVIEw,UITextFIEld,支持属性字符串,这意味着我们不需要创建CATextLayers作为我们的属性字符串的收件人。此外,为了使属性字符串,我们不需要再次使用CoreText :)我们在obj -c Foundation.framework中有新类,像NSParagraphStyle和其他常量,这将使我们的生活更轻松。好极了!
所以,如果我们有这个字符串:
Nsstring *text = @"Updated: 2012/10/14 21:59"
我们只需要创建属性字符串:
if ([_label respondsToSelector:@selector(setAttributedText:)]){ // iOS6 and above : Use NSAttributedStrings // Create the attributes const CGfloat FontSize = 13; NSDictionary *attrs = @{ NSFontAttributename:[UIFont boldSystemFontOfSize:FontSize],NSForegroundcolorAttributename:[UIcolor whitecolor] }; NSDictionary *subAttrs = @{ NSFontAttributename:[UIFont systemFontOfSize:FontSize] }; // Range of " 2012/10/14 " is (8,12). IDeally it shouldn't be hardcoded // This example is about attributed strings in one label // not about internationalisation,so we keep it simple :) // For internationalisation example see above code in swift const NSRange range = NSMakeRange(8,12); // Create the attributed string (text + attributes) NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attrs]; [attributedText setAttributes:subAttrs range:range]; // Set it in our UILabel and we are done! [_label setAttributedText:attributedText];} else { // iOS5 and below // Here we have some options too. The first one is to do something // less fancy and show it just as plain text without attributes. // The second is to use CoreText and get similar results with a bit // more of code. Interested people please look down the old answer. // Now I am just being lazy so :p [_label setText:text];}
有一些好的介绍性的博客文章here从家伙在invasivecode,解释与NSAttributedString更多的例子使用,寻找“介绍NSAttributedString为iOS 6”和“使用界面生成器的iOS的属性字符串”:)
PS:上面的代码它应该工作,但它是脑编译。我希望它是足够:)
iOS5及以下的旧答案
使用带有NSAttributedString的CATextLayer!比2 UILabels轻得多和简单。 (iOS 3.2及更高版本)
例。
不要忘记添加QuartzCore框架(需要CALayers)和CoreText(需要属性字符串)。
#import <QuartzCore/QuartzCore.h>#import <CoreText/CoreText.h>
下面的示例将添加一个子图层到导航控制器的工具栏。 àla Mail.app在iPhone。 总结
以上是内存溢出为你收集整理的iphone – 在单个UILabel中的粗体和非粗体文本?全部内容,希望文章能够帮你解决iphone – 在单个UILabel中的粗体和非粗体文本?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)