简体   繁体   English

使用swift根据文本中的字符创建NSMutableAttributedString

[英]Use swift to create NSMutableAttributedString based on character in text

I have an array of labels for my buttons: 我的按钮有一系列标签:

whenButtonArray = ["THURSDAY\nJUNE 5", "FRIDAY\nJUNE 6", "SATURDAY\nJUNE 7", "SUNDAY\nJUNE 8"]

What I'm trying to do is to use NSMutableAttributedString to make the text before the "\\n" be a bigger font than the text after it. 我想做的是使用NSMutableAttributedString来使“ \\ n”之前的文本大于其后的文本。

I initialized my string like this: 我这样初始化我的字符串:

myMutableString = NSMutableAttributedString(string: whenWhereButtonArray[i], attributes: [NSFontAttributeName:UIFont(name: "Tungsten-Book", size: 22.0)!])

But how can I make the second part of the string (after the \\n) be a smaller font size? 但是,如何使字符串的第二部分(在\\ n之后)成为较小的字体呢? I know I can do something like this: 我知道我可以做这样的事情:

myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Tungsten-Book", size: 12.0)!, range: NSRange(location:3,length:4))

But I need the range to be dynamic based on the text for each button. 但是我需要根据每个按钮的文本动态设置范围。 Thanks! 谢谢!

As long as there are no Unicode character representations/emojis within your string the following should work. 只要您的字符串中没有Unicode字符表示/表情符号,以下内容就应该起作用。 Otherwise it's best to work with NSString s as in rintaro's answer... 否则最好像rintaro的答案中那样使用NSString

In order to update the location and length of the remaining string dynamically as needed for your sub attribute, you have to start by getting the position of "\\n" (I've used the formula from this answer.): 为了根据子属性的需要动态更新剩余字符串的位置和长度,您必须首先获取“ \\ n”的位置(我已经使用了答案中的公式。):

let range = whenWhereButtonArray[i].rangeOfString("\n")
let newlineIndex : Int = distance(whenWhereButtonArray[i].startIndex, range.startIndex)

Then knowing the beginning index of "\\n" you can calculate (1) the second string's index and then (2) the length of the second string. 然后知道“ \\ n”的开始索引,您可以计算(1)第二个字符串的索引,然后(2)第二个字符串的长度。 ex: 例如:

let secondStringIndex : Int = newlineIndex + countElements("\n")
let lengthOfSecondString : Int = countElements(whenWhereButtonArray[i]) - secondStringIndex

Then you can enter that info into the sub attribute formula like so: 然后,您可以将信息输入到子属性公式中,如下所示:

myMutableString.addAttribute(NSFontAttributeName, value: UIFont(name: "Tungsten-Book", size: 12.0)!, range: NSRange(location:secondStringIndex,length:lengthOfSecondString))

In this case, working with NSString is relatively easier than working with Swift String : 在这种情况下,使用NSString比使用Swift String相对容易:

let whenButtonArray = ["THURSDAY\nJUNE 5", "FRIDAY\nJUNE 6", "SATURDAY\nJUNE 7", "SUNDAY\nJUNE 8"]

let str = whenButtonArray[0] as NSString // cast to `NSString`
let attributed = NSMutableAttributedString(string: str, attributes: [NSFontAttributeName:UIFont(name: "Arial", size: 22.0)!])
let nl = str.rangeOfString("\n")
if nl.location != NSNotFound {
    let range = NSMakeRange(NSMaxRange(nl), str.length - NSMaxRange(nl))
    attributed.addAttribute(NSFontAttributeName, value: UIFont(name: "Arial", size: 12.0)!, range: range)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM