Esq*_*uth 4 cocoa-touch ios swift
在Localizable.strings中
"rulesText" = "No illegal posting! \n No weird stuff! \n There is no tolerance for objectionable content, they will be removed!";
Run Code Online (Sandbox Code Playgroud)
我可以参与这个大胆的活动吗?喜欢没有奇怪的东西!或者在这个意义上使用unicode字符的东西?或者其他一些方式?
我这样使用它:
textView.text = "\n\n " + NSLocalizedString("rulesText", comment: "")
Run Code Online (Sandbox Code Playgroud)
在textView.text中使用时,更简单的方法是使用NSMutableAttributedString.这是一个例子:
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]
initWithString: NSLocalizedString("rulesText", comment: "")];
[attrString beginEditing];
[attrString addAttribute:kCTFontAttributeName
value:[[UIFont boldSystemFontOfSize:12] fontName]
range:NSMakeRange(2, 4)]; // use range as per your character index range
[attrString endEditing];
Run Code Online (Sandbox Code Playgroud)
使用它会自动加粗从第2个索引开始然后是4个字符的字符.例如:1234567890 - 12 3456 7890
希望这可以帮助.
对于SWIFT语言:
let font:UIFont? = UIFont.boldSystemFontOfSize(12.0)
myMutableString.addAttribute(NSFontAttributeName, value: font!, range: NSRange(location: 2, length: 4));
Run Code Online (Sandbox Code Playgroud)
http://www.raywenderlich.com/77092/text-kit-tutorial-swift
感谢Martins先前的回答,我编辑了他的解决方案以适合我的情况,并且效果很好。
签出并支持他的解决方案:在iOS的Swift中从纯文本(Android格式)中创建属性字符串
因此,这基本上发生了变化:
<a>hey</a> to size 14 bold
<b>hey</b> to size 12 bold
<u>hey</u> to underlined
Run Code Online (Sandbox Code Playgroud)
它很容易为它添加更多功能。
//localizable.strings
"rulesText" = "\n\n<a>The following will be removed</a> \n\n<b><u>Harassment</u></b>\n\nOther Stuff"
//viewdidload
textView.font = UIFont(name: "HelveticaNeue-Light", size: 12) //This is here to set up rest of the texts font
textView.attributedText = convertText(NSLocalizedString("rulesText", comment: ""))
//method for string conversation
func convertText(inputText: String) -> NSAttributedString {
var attrString = NSMutableAttributedString(string: inputText)
let boldFont = UIFont(name: "Helvetica-Bold", size: 12)
let boldBigFont = UIFont(name: "Helvetica-Bold", size: 14)
attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldFont!, propsIndicator: "<b>", propsEndIndicator: "</b>")
attrString = fixText(attrString, attributeName: NSFontAttributeName, attributeValue: boldBigFont!, propsIndicator: "<a>", propsEndIndicator: "</a>")
attrString = fixText(attrString, attributeName: NSUnderlineStyleAttributeName, attributeValue: NSUnderlineStyle.StyleDouble.rawValue, propsIndicator: "<u>", propsEndIndicator: "</u>")
return attrString
}
func fixText(inputText:NSMutableAttributedString, attributeName:AnyObject, attributeValue:AnyObject, propsIndicator:String, propsEndIndicator:String)->NSMutableAttributedString{
var r1 = (inputText.string as NSString).rangeOfString(propsIndicator)
while r1.location != NSNotFound {
let r2 = (inputText.string as NSString).rangeOfString(propsEndIndicator)
if r2.location != NSNotFound && r2.location > r1.location {
let r3 = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length)
inputText.addAttribute(attributeName as String, value: attributeValue, range: r3)
inputText.replaceCharactersInRange(r2, withString: "")
inputText.replaceCharactersInRange(r1, withString: "")
} else {
break
}
r1 = (inputText.string as NSString).rangeOfString(propsIndicator)
}
return inputText
}
Run Code Online (Sandbox Code Playgroud)