标签: nsmutableattributedstring

从NSAttributedString中提取最后50行

有没有一种简单的方法来拆分a,NSAttributedString以便我仅获得最后 50条左右的行?

NSMutableAttributedString *resultString = [receiveView.attributedText mutableCopy];
[resultString appendAttributedString:[ansiEscapeHelper attributedStringWithANSIEscapedString:message]];
if ([[resultString.string componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count]>50) {
    //resultString = [resultString getLastFiftyLines];
}
Run Code Online (Sandbox Code Playgroud)

split ios nsmutableattributedstring

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

iOS:更改NSMutableAttributedString的字体高度而不更改字体宽度?

我有一个NSMutableAttributedString"timeString"我已经设置的字体和颜色.一切都很好,但我想知道是否有办法改变文本的高度而不改变文本的宽度(字体宽度= 100%,字体高度= 120%).

我尝试使用transform,但它不适用于NSMutableAttributedStrings.谢谢!

fonts objective-c nsstring ios nsmutableattributedstring

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

带有emojis的NSAttributedString结束时未格式化

NSAttributedString在内容中使用表情符号的结尾没有被格式化.我正在尝试格式化整个字符串(在此示例中将文本颜色设置为白色,以简化),但是当放入文本时,某些文本保持未格式化UILabel.

在此输入图像描述

目前,我正在使用

let attributedString = NSMutableAttributedString(string: contents)

attributedString.addAttribute(
    NSForegroundColorAttributeName,
    value: UIColor.white,
    range: NSMakeRange(0, contents.characters.count)
)

label.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)

我也试过通过使用contents.utf8.count获得长度,但获得相同的结果.

我注意到未格式化的字符数与字符串中的表情符号数相同.这可能与正在发生的事情有关吗?

nsattributedstring uilabel ios nsmutableattributedstring swift

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

如何将NSTextView限制为2行?

我正在尝试为NSTextView指定行数.我的设计师要求最多两行文字.我已经尝试过NSMutableParagraph样式来添加我想要的省略号截断,但是使用NSMutableParagraph我只能得到带有1行的NSTextView而没有NSMutableParagraph,我得到一个滚动文本,其中包含完成文本所需的行数.

var attributedString = NSMutableAttributedString(string: "This is my text, I can keep going for many characters")
var para = NSMutableParagraphStyle()
para.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let globalAttributes = [
  NSParagraphStyleAttributeName: para
]
let range = NSRange(location:0, length: attributedString.length)
attributedString.addAttributes(globalAttributes, range: range)
cellView.myTextView!.textStorage?.setAttributedString(attributedString)
Run Code Online (Sandbox Code Playgroud)

我在NSTextView上尝试过高度限制.我试过了:

cellView.myTextView!.textContainer?.containerSize = NSMakeSize(300, 32)
Run Code Online (Sandbox Code Playgroud)

我已经尝试为NSScrollView创建IBOutlet NSTextView并调整其高度.获得两条线和截断都没有运气.任何帮助是极大的赞赏.我觉得我只是错过了方法或设置.谢谢!

macos cocoa nstextview nsmutableattributedstring nsparagraphstyle

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

识别标签中显示的属性字符串中的图像点击

我有一个如下所示的属性字符串要显示在标签中。

let someText = NSMutableAttributedString(string: "This is a sample text with")
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named: "icon") 

let imageString = NSAttributedString(attachment: imageAttachment)
someText.append(imageString)
someText.append(NSAttributedString(string: "attached")
somelabel.attributedText = someText
Run Code Online (Sandbox Code Playgroud)

标签显示This is a sample text with 'image' attached

如何识别点击图像(而不是文本)来执行操作?

xcode uilabel ios nsmutableattributedstring swift

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

在Swift中为NSMutableAttributedString设置kCTUnderlineStyleAttributeName

kCTUnderlineStyleAttributeName的文档会转发以下内容:

kCTUnderlineStyleAttributeName 对于此属性适用的文本,在渲染时应用的下划线样式.值必须是CFNumber对象.默认值为kCTUnderlineStyleNone.设置除kCTUnderlineStyleNone之外的值的值以绘制下划线.此外,CTUnderlineStyleModifiers中列出的常量可用于修改下划线的外观.下划线颜色由文本的前景色决定.

setAttributes函数的签名如下:

func setAttributes(attrs: [NSObject : AnyObject]?, range: NSRange)
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是文档似乎暗示CTUnderlineStyle.Single可以(并且应该在Swift中)用作kCTUnderlineStyleAttributeName键的值.但是,前者是结构,因此不符合字典值类型所需的AnyObject协议.

有任何想法吗?

ios nsmutableattributedstring swift

4
推荐指数
1
解决办法
1318
查看次数

iOS - 如何在swift中使用`NSMutableString`

我已经看过这个Objective-C代码,但我很难在swift中做同样的事情:

NSMutableAttributedString *res = [self.richTextEditor.attributedText mutableCopy];

[res beginEditing];
__block BOOL found = NO;
[res enumerateAttribute:NSFontAttributeName inRange:NSMakeRange(0, res.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
    if (value) {
        UIFont *oldFont = (UIFont *)value;
        UIFont *newFont = [oldFont fontWithSize:oldFont.pointSize * 2];
        [res removeAttribute:NSFontAttributeName range:range];
        [res addAttribute:NSFontAttributeName value:newFont range:range];
        found = YES;
    }
}];
if (!found) {
    // No font was found - do something else?
}
[res endEditing];
self.richTextEditor.attributedText = res;
Run Code Online (Sandbox Code Playgroud)

我试图NSMutableAttributedString通过迭代每个属性来改变a中的字体.我很高兴听到有更好的方法,但如果有人能帮助我翻译上述内容,我会感到非常满意.

xcode font-size ios nsmutableattributedstring swift

4
推荐指数
2
解决办法
5399
查看次数

将NSAttributedString添加到UIBarButtonItem

我正在尝试在后栏按钮项目上设置属性字符串.
这是我第一次尝试归因于字符串.
这是代码:

    self.navigationItem.hidesBackButton = true
    let barButtonBackStr = "< Back"
    var attributedBarButtonBackStr = NSMutableAttributedString(string: barButtonBackStr as String)
    attributedBarButtonBackStr.addAttribute(NSFontAttributeName,
        value: UIFont(
            name: "AmericanTypewriter-Bold",
            size: 18.0)!,
        range: NSRange(
            location:0,
            length:1))
    let newBackButton = UIBarButtonItem(title: attributedBarButtonBackStr, style: UIBarButtonItemStyle.Plain, target: self, action: "barButtonBack:")
    self.navigationItem.leftBarButtonItem = newBackButton
Run Code Online (Sandbox Code Playgroud)

这导致Xcode中出现以下错误.

无法使用类型为'(title:NSMutableAttributedString,style:UIBarButtonItemStyle,target:CombatOutcomeViewController,action:String)的参数列表调用类型'UIBarButtonItem'的初始值设定项'

任何人都知道如何做到这一点?谢谢.

uibarbuttonitem nsmutableattributedstring swift

4
推荐指数
1
解决办法
3610
查看次数

如何将系统字体与属性字符串一起使用?

let attributedText: NSMutableAttributedString = NSMutableAttributedString(string: text, attributes: [NSFontAttributeName: UIFont(name: "San Francisco", size: 14.0)!])
Run Code Online (Sandbox Code Playgroud)

使用上面的代码,当我运行我的应用程序时,它崩溃了,因为它找不到“旧金山”字体。但这不是iOS 9的系统字体吗?如何在属性字符串中使用系统字体?

我正在使用 Swift 2.2

fonts nsattributedstring nsmutableattributedstring swift swift2.2

4
推荐指数
1
解决办法
4370
查看次数

NSMutableParagraphStyle with NSMutableAttributedString

我有一个段落正在使用NSMutableParagraphStyle管理行高。另外,我想在段落中更改一个单词的颜色,这是我正在使用的代码,但它只是更改了一个单词的颜色(attributedText被覆盖),我该如何解决?有帮助吗?

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineHeightMultiple = 1.45f;

paragraphStyle.alignment = UITextAlignmentRight;

NSDictionary *ats = @{
                      NSFontAttributeName            : [UIFont fontWithName:@"Helvetica"
                                                                      size:currentSize],
                      NSParagraphStyleAttributeName  : paragraphStyle,

                      };


ayaTxt.attributedText    = [[NSAttributedString alloc] initWithString:ayaTxt.text
                                                             attributes:ats];

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]
                                         initWithString:ayaTxt.text];

NSRange range = [[AyatWords objectAtIndex:aya] rangeOfString:@":"];

[attrString addAttribute:NSForegroundColorAttributeName
                   value:[UIColor colorWithRed:(25.0/255.0)
                                         green:(168.0/255.0)
                                          blue:(167.0/255.0)
                                         alpha:1.0]
                   range:NSMakeRange(0, range.location + 1)];

ayaTxt.attributedText = attrString;
Run Code Online (Sandbox Code Playgroud)

谢谢 ..

objective-c uitextview uicolor nsmutableattributedstring

3
推荐指数
1
解决办法
2998
查看次数