18

I am digging into NSAttributedStrings on iOS. I have a model that is returning a persons first and last name as NSAttributesString. (I don't know if it is a good idea to deal with attributed strings in models!?) I want the first name to be printed regular where as the last name should be printed in bold. What I don't want is to set a text size. All I found so far is this:

- (NSAttributedString*)attributedName {
    NSMutableAttributedString* name = [[NSMutableAttributedString alloc] initWithString:self.name];
    [name setAttributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:[UIFont systemFontSize]]} range:[self.name rangeOfString:self.lastname]];
    return name;
}

However, this will, of course, override the font size of the last name, which gives a very funny look in a UITableViewCell where the first name will be printed in the regular text size of the cell's label and the last name will be printed very small.

Is there any way to achieve what I want?

Thanks for your help!

4

6 回答 6

6

这是一个Swift extension使文本变粗的方法,同时保留当前的字体属性(和文本大小)。

public extension UILabel {

    /// Makes the text bold.
    public func makeBold() {
        //get the UILabel's fontDescriptor
        let desc = self.font.fontDescriptor.withSymbolicTraits(.traitBold)
        //Setting size to '0.0' will preserve the textSize
        self.font = UIFont(descriptor: desc, size: 0.0)
    }

}
于 2015-10-28T04:34:55.380 回答
6

StrokeWidth通过将属性设置为负值,您可以在不更改其他属性的情况下将字符串加粗。

目标-C:

[name setAttributes:@{NSStrokeWidthAttributeName : @-3.0} range:NSRangeFromString(name.string)];

迅速:

name.setAttributes([.strokeWidth: NSNumber(value: -3.0)], range: NSRangeFromString(name.string))
于 2018-06-23T13:48:25.257 回答
3

使用这个问题的技术:

UIFontDescriptor *fontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody];
uint32_t existingTraitsWithNewTrait = UIFontDescriptorTraitBold;
    fontDescriptor = [fontDescriptor fontDescriptorWithSymbolicTraits:existingTraitsWithNewTrait];
UIFont *updatedFont = [UIFont fontWithDescriptor:fontDescriptor size:0.0];
NSDictionary *attribs =  @{NSFontAttributeName : updatedFont};
[mutableAttrString setAttributes:attribs range:result.range];
于 2015-04-03T04:31:51.643 回答
2

从表格单元格代码进行上述调用,但通过从单元格的 textLabel 获取字体大小来传递所需的字体大小。

于 2012-10-19T15:06:27.693 回答
2

您是否尝试将大小设置为 0.0?

于 2014-09-24T15:56:38.247 回答
2

如果您只想在字符串标签中加粗第二个单词,即使用默认 iOS 系统字体时的名称标签,即标题或标题等

let s = "\(client.givenName) \(client.surname)" as NSString

let myAttribute = [ NSFontAttributeName: UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1) ]
let myString = NSMutableAttributedString(string: "\(s)", attributes: myAttribute )

let myRange = s.rangeOfString(client.surname)
let desc = UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1).fontDescriptor().fontDescriptorWithSymbolicTraits(.TraitBold)
let new = UIFont(descriptor: desc, size: 0.0)

myString.addAttribute(NSFontAttributeName, value: new, range: myRange)

nameLabel.attributedText = myString
于 2016-01-11T09:59:16.483 回答