标签: nsmutableattributedstring

多线UILabel上的NSAttributedString切断第一线

我已经看了大约10个关于SO的问题,而且我仍然很短.

我有一个多行UILabel(在Interface Builder中创建,numberOfLines设置为0),它通常像这样呈现:

在此输入图像描述

我想强调"服务条款"和"隐私政策",所以我添加了以下代码:

NSString *text = self.agreement.text;
NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:text];

NSRange privacyRange = [text rangeOfString:@"privacy policy" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:privacyRange];

NSRange tosRange = [text rangeOfString:@"terms of service" options:NSCaseInsensitiveSearch];
[aString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:tosRange];

self.agreement.attributedText = aString;
Run Code Online (Sandbox Code Playgroud)

但结果如下:

在此输入图像描述

我需要做什么才能显示这两行,并用适当的范围加下划线?

此外,我不想使用像OHAttributedLabel或TTTAttributedLabel这样的第三方库,因为这是我的应用中唯一需要为一段文字加下划线的地方.

我试过的

  • sizeToFit设置属性文本后调用
  • 改为使用UITextView.两条线都正确渲染但我丢失了中心对齐方式.
  • 重置numberOfLineslineBreakMode代码

Sascha让我上传了两个截图,背景颜色设置为清除之外的其他颜色.奇怪的是,一切都按预期显示!不知道该怎么做或这告诉我们什么.

在此输入图像描述

objective-c nsattributedstring uilabel ios nsmutableattributedstring

6
推荐指数
1
解决办法
4390
查看次数

tableviewcell中的归属字符串在单元格出列并重新加载之前不显示粗体文本

我有一个表视图,其中单元格有一个带有一些属性文本的标签.正确设置了文本cellForRowAtIndexPath.正确设置了文本的颜色,但在单元格出列之前,不会显示粗体属性.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        MyCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
        Model *model = [self.fetchedResultsController objectAtIndexPath:indexPath];
        cell.tag = indexPath.row;
        [cell updateContentWithModel:model atIndexPath:indexPath];
        return cell;
}


- (void)updateContentWithModel:(Model *)model atIndexPath:(NSIndexPath *)indexPath
{
self.model = model;
[self setTextFromModel];
[self setImageFromModelAtIndexPath:indexPath];
}

- (void) setTextFromModel
{
self.title.text = self.model.header;
self.status.attributedText = [self boldString:self.model.status fontSize:self.status.font.pointSize color:[UIColor redColor]];
}

+(NSMutableAttributedString *)boldString:(NSString *)stringToBold fontSize:(CGFloat)fontSize color:(UIColor *)color
{
NSMutableAttributedString *boldString = [[NSMutableAttributedString alloc] initWithString:stringToBold];
[boldString setAttributes:@{NSForegroundColorAttributeName: color,
                            NSFontAttributeName:[UIFont boldSystemFontOfSize:fontSize]} range:NSMakeRange(0, boldString.length)];
return boldString;
} …
Run Code Online (Sandbox Code Playgroud)

uitableview ios nsmutableattributedstring

6
推荐指数
1
解决办法
1359
查看次数

SKLabelNode + NSMutableAttributedString = 无阴影

    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = NSTextAlignment.right
    paragraph.tailIndent = -3
    let shadow = NSShadow()
    shadow.shadowBlurRadius = 5
    shadow.shadowColor = UIColor.gray
    shadow.shadowOffset = CGSize(width: 2, height: -2)
    let text = NSMutableAttributedString(string: "MY LABEL", attributes:
   [NSShadowAttributeName : shadow,
    NSStrokeColorAttributeName : UIColor.black,
    NSStrokeWidthAttributeName : -3,
    NSForegroundColorAttributeName: UIColor.white,
    NSFontAttributeName : UIFont(name: "MyFont", size: 24) as Any,
    NSParagraphStyleAttributeName : paragraph])

    let node = SKLabelNode()
    addChild(node)
    node.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)

它有效,但没有阴影。我可以使用相同的NSMutableAttributedString上著名的ASAttributedLabel和它的作品不错,有一个影子,但我想使用SKLabelNode,以实现更好的性能IOS11ASAttributedLabel可能会滞后于动态场景:(

nsattributedstring nsmutableattributedstring sprite-kit sklabelnode swift

6
推荐指数
0
解决办法
375
查看次数

为什么带有线间距的多线UILabel会删除最后的3个点?

我在向UILabel添加linespacing方面遇到了问题.如果我不使用linepacing,如果文本溢出,我会在第3行的末尾得到3个点.

    UILabel *labelBlurb = [[UILabel alloc] initWithFrame:CGRectMake(marginLeft, 15+20, 295, 60)];
    [labelBlurb setNumberOfLines:3];
    [labelBlurb setText:blurb];
    [labelBlurb setLineBreakMode:NSLineBreakByTruncatingTail];
    [labelBlurb setAdjustsFontSizeToFitWidth:NO];
    [labelBlurb setTextColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0]];
    [labelBlurb setBackgroundColor:[UIColor clearColor]];
    [labelBlurb setFont:[UIFont fontWithName:@"HelveticaNeue" size:12]];
Run Code Online (Sandbox Code Playgroud)

但是当我添加这样的属性文本时:

    attributedString = [[NSMutableAttributedString alloc] initWithString:blurb];
    paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3.5];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [blurb length])];
    labelBlurb.attributedText = attributedString;
    [view addSubview:labelBlurb];
Run Code Online (Sandbox Code Playgroud)

最后的3个点消失了.添加attributedText时如何防止删除3个点?

这就是我想 3.5 linespacing:

培根ipsum dolor坐在amet doner猪肚leberkas熏牛肉.

短腰熏牛肉ribeye boudin里脊肉.肩部排骨牛肉

培根.萨拉米香肠干酪舌头火腿牛肉排骨肉丸.. < - 3点

uilabel ios nsmutableattributedstring ios7

5
推荐指数
1
解决办法
1725
查看次数

动画 NSMutableAttributedString 的部分更改

我正在制作一个具有 UITextView 的 iOS 应用程序。在该 UITextView 中关闭括号时,我想向用户突出显示它与哪个左括号配对。到目前为止,我已经使用 NSMutableAttributedString 并更改了成对括号的字体大小来完成此操作,这可行但有点难看。我真正想要的是在我的代码中关闭括号时,类似于 xcode 做同样事情的方式来设置动画有没有办法做到这一点?

非常感谢任何帮助,尽管我对此很陌生,所以请不要假设我知道太多:)

这是我的代码:

    @IBAction func didPressClosingParentheses(sender: AnyObject) {
    appendStringToInputTextView(")")
    var count = 1
    let currentString = inputTextView.attributedText.string
    let characterArray = Array(currentString)
    let closingIndex = characterArray.count - 1
    for i in reverse(0...closingIndex-1) {
        if characterArray[i] == "(" {
            count--
        }
        else if characterArray[i] == ")" {
            count++
        }
        if count == 0 {
            let startingIndex = i
            var newString = NSMutableAttributedString(string: currentString)
            newString.addAttribute(NSFontAttributeName, value: …
Run Code Online (Sandbox Code Playgroud)

animation ios nsmutableattributedstring swift

5
推荐指数
1
解决办法
3157
查看次数

使用 NSMutableAttributedString 实现圆角

我认为我的问题很简单,但我在谷歌上找不到任何东西。我用来NSMutableAttributedString修改字符串中的样式,例如我使用此代码来更改背景颜色:

    [string addAttribute:NSBackgroundColorAttributeName value:[UIColor colorWithRed:0.4 green:0.8 blue:1 alpha:1] range:NSMakeRange(0,18)];
Run Code Online (Sandbox Code Playgroud)

我的问题是,我如何才能圆化这个背景的角落?
提前致谢。

objective-c ios nsmutableattributedstring

5
推荐指数
0
解决办法
3754
查看次数

用粗体和斜体设置标签字体

我正在尝试使用 将标签的特定部分设置为粗体和斜体NSMutableAttributedString,但我只能得到一个或另一个。我使用的字体是 Clear Sans。将故事板上的整个标签字体设置为Bold Italic正常工作,但我只需要其中的一部分。所以我用

for family in UIFont.familyNames() {
    print("\(family)\n\t\(UIFont.fontNamesForFamilyName(family))")
}
Run Code Online (Sandbox Code Playgroud)

以确保它在那里,我发现

Clear Sans
    ["ClearSans", "ClearSans-Bold", "ClearSans-Italic"]
Run Code Online (Sandbox Code Playgroud)

出于某种原因,它不在此处,而是在故事板中。所以我尝试使用这个答案,但它崩溃了,我认为与我没有找到的原因相同ClearSans-BoldItalic,但我不知道为什么它不在那里。

最后,我试着像这样“堆叠”两者

let bold = UIFont(name: "ClearSans-Bold", size: 20)
let italic = UIFont(name: "ClearSans-Italic", size: 20)

attributedCaption.addAttribute(NSFontAttributeName, value: bold!, range: (imageContainer.caption as NSString).rangeOfString(matchString))
attributedCaption.addAttribute(NSFontAttributeName, value: italic!, range: (imageContainer.caption as NSString).rangeOfString(matchString))
Run Code Online (Sandbox Code Playgroud)

但所有这些都是使用最后一个属性,这是我期望它做的。

是否有其他方法可以用来执行此操作,以便在标签的同一部分同时显示粗体和斜体?

编辑

我想包含项目中的字体,所以很明显我已经导入了它们。第二张图片是故事板上的字体列表。虽然它被掩盖了,但这个家庭实际上是Clear Sans

安装的字体 故事板选项

fonts ios nsmutableattributedstring swift

5
推荐指数
1
解决办法
8613
查看次数

使用自定义字体时 UITextView 属性文本不起作用

我正在使用UITextView并通过代码设置其字体和属性文本。

案例 1: 自定义字体 - 是归属文本- 否

在此输入图像描述

textView 文本以自定义字体显示。

我想要带有自定义字体的属性文本,即消息:在这种情况下应加粗。

案例 2: 自定义字体 - 是归属文本- 是

在此输入图像描述

仅属性文本(粗体文本)以自定义字体显示。

我的代码是:

- (void)loadTextView
{
    _textView.text=NSLocalizedString(@"more_info_text",nil);
    _textView.font=[UIFont fontWithName:@"BurbankSmall-Medium" size:16];

    NSRange rangeBold  = [_textView.text rangeOfString:@"Examples of invalid characters:"];
    NSRange rangeBold2 = [_textView.text rangeOfString:@"Restricted text:"];
    NSRange rangeBold3 = [_textView.text rangeOfString:@"Message :"];

    UIFont *fontText = [self boldFontWithFont:_textView.font];
    NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];

    NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text];
    [mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];
    [mutAttrTextViewString setAttributes:dictBoldText range:rangeBold2];
    [mutAttrTextViewString …
Run Code Online (Sandbox Code Playgroud)

uitextview uifont ios nsmutableattributedstring

5
推荐指数
1
解决办法
7357
查看次数

更改 UIAlertController 的标题字体大小

我试图更改标题fontSize中的UIAlertController,但我不能管理如何我设置NSMutableAttributedStringtitle-属性。

所以我一直在NSMutableAttributedString使用以下代码创建:

let title = NSMutableAttributedString(string: user.fullName)
let range = NSRange(location: 0, length: title.length)
title.addAttribute(NSAttributedStringKey.font, value: UIFont.TextStyle.largeTitle, range: range)
Run Code Online (Sandbox Code Playgroud)

现在对我来说棘手的部分是如何弄清楚如何将新标题设置为UIAlertController,因为它需要一个String?值。

我环顾四周,发现UILabel在呈现UIAlertController. 但是如何使用我自己的自定义覆盖title-property ?UIAlertControllerUILabel

present(myUIAlertController, animated: true) {
    // Creating the UILabel depending on string length
    // Set the NSMutableAttributedString value to the custom UILabel and override title property.
}
Run Code Online (Sandbox Code Playgroud)

或者也许有更简单的方法来解决这个问题?


我的目标是像下图一样:

带有大标题的 UIAlertViewController

nsmutableattributedstring swift uialertviewcontroller swift4

5
推荐指数
1
解决办法
6471
查看次数

如何向 NSMutableAttributedString 添加填充?

我有一个标签,它使用 NSMutableAttributedString 将文本写为:

在此处输入图片说明

我想要做的是降低星号的顶部填充,以便它的 midY 与下面的 Cuisine 一词一致:

在此处输入图片说明

如何使用 NSMutableAttributedString 添加填充?

我知道我可以单独使用星号创建一个单独的标签,并使用带有常量的锚点将其居中,但我想看看使用 NSMutableAttributedString 是如何实现的

let cuisineLabel: UILabel = {
    let label = UILabel()
    label.translatesAutoresizingMaskIntoConstraints = false

    let attributedText = NSMutableAttributedString(string: "Cuisine ", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17), NSAttributedStringKey.foregroundColor: UIColor.lightGray])

    attributedText.append(NSAttributedString(string: "*", attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 24), NSAttributedStringKey.foregroundColor: UIColor.red]))

    label.attributedText = attributedText

    return label
}()
Run Code Online (Sandbox Code Playgroud)

padding uilabel ios nsmutableattributedstring swift

5
推荐指数
2
解决办法
5673
查看次数