NSAttributedString change style to bold without changing pointSize?

こ雲淡風輕ζ 提交于 2019-12-06 17:18:25

问题


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!


回答1:


Here's a Swift extension that makes text bold, while preserving the current font attributes (and text size).

public extension UILabel {

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

}



回答2:


Use technique from this question:

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];



回答3:


Make the above call from the table cell code but pass in the desired font size by getting the font size from the cell's textLabel.




回答4:


If you just want to Bold the second word in a string label, i.e. for a name label while using the Default iOS System Fonts, i.e. heading, or title etc

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



回答5:


Have you tried setting the size to 0.0?




回答6:


You can bold a string without changing its other attributes by setting the StrokeWidth attribute with a negative value.

Objective-C:

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

Swift:

name.setAttributes([.strokeWidth: NSNumber(value: -3.0)], range: NSRangeFromString(name.string))


来源:https://stackoverflow.com/questions/12974120/nsattributedstring-change-style-to-bold-without-changing-pointsize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!