标签: nsattributedstring

NSAttributedString插入一个项目符号点?

所以我有一个NSAttributedString我想bullet point在一部分文本的开头插入一个.我怎样才能做到这一点?如何CTPAragraphStyle在显示文本时创建创建此项目符号点?

编辑: 应该可以在iOS上使用

objective-c bulletedlist nsattributedstring ios swift

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

如何使用NSAttributedString制作下标和上标?

我需要为化学公式(H2O,Na ^ 2 +等)做下标吗?

这可能与NSAttributedString有关,还是有替代/更简单的方式来制作下标?

objective-c nsstring nsattributedstring

25
推荐指数
3
解决办法
2万
查看次数

NSParagraphStyle行间距被忽略

一个失败的简单测试:使用一个子视图(UITextView)创建一个新项目并将以下内容放入:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineHeightMultiple = 50.f;
    paragraphStyle.lineSpacing = 100.f;
    paragraphStyle.minimumLineHeight = 200.f;
    paragraphStyle.maximumLineHeight = 500.f;

    UIFont *font = [UIFont fontWithName:@"AmericanTypewriter" size:24.f];

    self.textView.attributedText = [[NSAttributedString alloc] initWithString:
    @"This is a test.\n Will I pass?" attributes:
    @{NSParagraphStyleAttributeName : paragraphStyle, NSFontAttributeName : font}];
}
Run Code Online (Sandbox Code Playgroud)

行间距与属性不存在时相同.有没有什么能成功地运作?我提出荒谬的数字只是为了表明它不会改变......

objective-c uitextview nsattributedstring ios6

24
推荐指数
4
解决办法
4万
查看次数

在UIView的-drawRect:方法中绘制彩色文本

我试图在我的UIView子类中绘制彩色文本.现在我正在使用Single View应用程序模板(用于测试).除drawRect:方法外没有任何修改.

绘制文本但无论我将颜色设置为什么,它总是黑色的.

- (void)drawRect:(CGRect)rect
{
    UIFont* font = [UIFont fontWithName:@"Arial" size:72];
    UIColor* textColor = [UIColor redColor];
    NSDictionary* stringAttrs = @{ UITextAttributeFont : font, UITextAttributeTextColor : textColor };

    NSAttributedString* attrStr = [[NSAttributedString alloc] initWithString:@"Hello" attributes:stringAttrs];

    [attrStr drawAtPoint:CGPointMake(10.f, 10.f)];
}
Run Code Online (Sandbox Code Playgroud)

我也试过[[UIColor redColor] set]无济于事.

回答:

NSDictionary*stringAttrs = @ {NSFontAttributeName:font,NSForegroundColorAttributeName:textColor};

uikit nsattributedstring uicolor drawrect ios

24
推荐指数
1
解决办法
2万
查看次数

使用换行符附加NSAttributedString会返回格式错误的属性字符串

我正在使用NSMutableAttributedStringNSAttributedString以两种不同的字体大小显示标签文本.我的方法是:

NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:@"2"];
NSAttributedString *atrStr = [[NSAttributedString alloc]initWithString:@"days" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[muAtrStr appendAttributedString:atrStr];
Run Code Online (Sandbox Code Playgroud)

它返回一个归因字符串,字体大小为12,字符大小为"2",字体大小为8.

但是,另一种情况是在2之后添加换行符.我使用以下代码:

NSMutableAttributedString *muAtrStr = [[NSMutableAttributedString alloc]initWithString:@"2"];
NSAttributedString *atrStr = [[NSAttributedString alloc]initWithString:@"\ndays" attributes:@{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Bold" size:8]}];
[muAtrStr appendAttributedString:atrStr];
Run Code Online (Sandbox Code Playgroud)

此时属性字符串应用全文的属性.我得到一个字体大小为8的"2 \ndays"的属性字符串.

objective-c nsattributedstring ios

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

如何在NSTextAttachment中设置模板化图像的颜色

如何设置作为属性字符串附件的模板化图像的颜色?

背景:

我有一个UILabel,我将其attributionText设置为NSAttributedString.NSAttributedString包含带有小图像的NSTextAttachment.现在我想让我的图像颜色与文本颜色匹配,我无法弄清楚如何使它工作.

我通常希望通过将其渲染模式设置为UIImageRenderingModeAlwaysTemplate然后在包含UIView上设置tintColor来为图像着色.我试过在我的UILabel上设置tintColor但是没有效果.

这是我的代码.它在Ruby(RubyMotion)中,因此语法可能看起来有点滑稽,但它与Objective C 1:1映射.

attachment = NSTextAttachment.alloc.initWithData(nil, ofType: nil)
attachment.image = UIImage.imageNamed(icon_name).imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate)

label_string = NSMutableAttributedString.attributedStringWithAttachment(attachment)
label_string.appendAttributedString(NSMutableAttributedString.alloc.initWithString('my text', attributes: { NSFontAttributeName => UIFont.preferredFontForTextStyle(UIFontTextStyleFootnote), NSForegroundColorAttributeName => foreground_color }))

label = UILabel.alloc.initWithFrame(CGRectZero)
label.tintColor = foreground_color
label.attributedText = label_string
label.textAlignment = NSTextAlignmentCenter
label.numberOfLines = 0
Run Code Online (Sandbox Code Playgroud)

nsattributedstring nstextattachment ios

24
推荐指数
4
解决办法
6622
查看次数

UILabel中的NSAttributedString尾截断

我正在使用ContextLabel来解析@,#和URL.这是我发现的最佳解决方案,因为它的尺寸正确并且不会影响性能.它首先在输入处解析字符串,然后将其转换为NSAttributedString和之后将其分配给attributedText属性UILabel.一切都按预期工作,除了尾部截断 - 它是非常不正确的(见下图)

在此输入图像描述

我应该从哪里开始挖掘 - 归因于字符串的属性是错误的吗?还是标签布局问题?谢谢!

nsattributedstring uilabel ios hashtag swift

24
推荐指数
2
解决办法
6096
查看次数

如何在NSAttributedString上设置字体大小

编辑 - 这已被标记为重复,但正如我在下面所述,我正在寻找一个Swift解决方案.我发现的一切都是用Objective C写的.

我试图将HTML转换为NSAttributedString,但无法确定如何设置字体样式和大小.所有示例都在Objective C中,我不熟悉.如何将字体样式/大小设置为System 15(作为示例)

private func stringFromHtml(string: String) -> NSAttributedString? {
        do {
            let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
            if let d = data {
                let str = try NSAttributedString(data: d,
                                                 options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],                                                  documentAttributes: nil)
                return str
            }
        } catch {
        }
        return nil
    }
Run Code Online (Sandbox Code Playgroud)

编辑......我尝试了几种方法.我无法让它发挥作用.我现在收到一条错误消息:表达式类型不明确,没有更多上下文.它指向NSAttributedString我尝试了以下内容:

let myAttribute = [ NSForegroundColorAttributeName: UIColor.blue ]
let data = string.data(using: String.Encoding.utf8, allowLossyConversion: true)
  if let d = data {
     let str = try NSAttributedString(data: d,
                       attributes: myAttribute,
                       options: …
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring ios

24
推荐指数
3
解决办法
3万
查看次数

iOS NSAttributedString为HTML

我有一个NSAttributed字符串(来自HTML)我设置为UITextView.

- (void)setHtml:(NSString *)html {

    NSData *htmlData = [html dataUsingEncoding:NSUTF8StringEncoding];

    // Create the HTML string
    NSDictionary *importParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSError *error = nil;
    self.htmlString = [[NSAttributedString alloc] initWithData:htmlData options:importParams documentAttributes:NULL error:&error];

    self.editorView.attributedText = self.htmlString;

}
Run Code Online (Sandbox Code Playgroud)

然后我让用户编辑他们想要的内容,然后我想再将其转换为HTML,所以我使用:

- (NSString *)getHTML {
    NSDictionary *exportParams = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};
    NSData *htmlData = [self.editorView.attributedText dataFromRange:NSMakeRange(0, self.editorView.attributedText.length) documentAttributes:exportParams error:nil];
    return [[NSString alloc] initWithData:htmlData encoding:NSUTF8StringEncoding];
}
Run Code Online (Sandbox Code Playgroud)

它确实返回HTML,但它不是我想要的.一切都被赋予了一个类属性,以及它放在文档顶部的CSS.图像和链接之类的东西甚至不包含在返回的HTML中,可能还有更多问题.

有没有更好的方法来获取HTML NSAttributedString?或者,有没有办法解析NSAttributedString并编写自己的HTML?

objective-c uitextview nsattributedstring ios textkit

23
推荐指数
2
解决办法
8087
查看次数

如何调整图像大小或作为NSAttributedString NSTextAttachment完成(或设置其初始大小)

我有一个NSAttributedString我要添加一个NSTextAttachment.图像是50w乘50h,但我希望它缩小以反映属性字符串的行高.我以为这会自动完成,但我猜不会.我查看了UImage类引用,但是这个图像似乎没有在UIImageView中设置,因此无法访问框架属性.这是我目前拥有的截图:

在此输入图像描述

在理想的世界中,我还想实现一种基于用户输入(例如增加字体大小)来放大图像的方法.关于如何实现这一点的任何想法?

谢谢

编辑1

这是我如何创建它:

    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"note-small.png"];
    NSLog(@"here is the scale: %f", textAttachment.image.scale);
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withString:@" "];
    [headerAS replaceCharactersInRange:NSMakeRange([headerAS length], 0) withAttributedString:attrStringWithImage];
Run Code Online (Sandbox Code Playgroud)

uiimage nsattributedstring nstextattachment ios ios7

23
推荐指数
3
解决办法
2万
查看次数