NSAttributedString将样式更改为粗体而不更改切入点大小?

时间:2022-12-03 16:25:37

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:

我正在研究iOS上的nsattributedstring。我有一个模型,它以NSAttributesString的形式返回person的姓和名。(我不知道在模型中处理属性字符串是否是个好主意!)我想要把名字打印成规则,因为名字应该用粗体打印。我不想设置文本大小。到目前为止,我发现的是:

- (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.

然而,这当然会覆盖姓氏的字体大小,这在UITableViewCell中看起来非常有趣,其中第一个名字将以单元格标签的常规文本大小打印,而最后一个名字将打印得非常小。

Is there any way to achieve what I want?

有没有办法实现我想要的?

Thanks for your help!

谢谢你的帮助!

6 个解决方案

#1


3  

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

#2


3  

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)
    }

}

#3


2  

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.

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

#4


2  

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

如果您只是想要在字符串标签中加粗第二个单词,例如在使用默认的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

#5


1  

Have you tried setting the size to 0.0?

你试过把尺寸设为0吗?

#6


0  

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

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

Objective-C:

objective - c:

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

Swift:

迅速:

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

#1


3  

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

#2


3  

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)
    }

}

#3


2  

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.

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

#4


2  

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

如果您只是想要在字符串标签中加粗第二个单词,例如在使用默认的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

#5


1  

Have you tried setting the size to 0.0?

你试过把尺寸设为0吗?

#6


0  

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

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

Objective-C:

objective - c:

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

Swift:

迅速:

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