标签: nsparagraphstyle

在UILabel中的单词分隔符上添加连字符

如何设置UILabel lineBreakMode来破坏单词并为断字添加连字符?

一个破碎的标签 -

rd应该是这样的

hyphen word-wrap uilabel ios nsparagraphstyle

17
推荐指数
3
解决办法
9772
查看次数

在UITextView - NSAttributedString中绘制一条线

我希望在UITextView一些文本组成的内容中绘制一个可自定义的行(使用NSAttributedString)

这是我尝试过的

NSString *unicodeStr = [NSString stringWithFormat:@"%C%C%C", 0x00A0, 0x0009, 0x00A0]; //nbsp, tab, nbsp
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:unicodeStr];
NSRange strRange = NSMakeRange(0, str.length);

NSMutableParagraphStyle *const tabStyle = [[NSMutableParagraphStyle alloc] init];
tabStyle.headIndent = 16; //padding on left and right edges
tabStyle.firstLineHeadIndent = 16;
tabStyle.tailIndent = -16;
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:40 options:@{}]; //this is how long I want the line to be
tabStyle.tabStops = @[listTab];
[str  addAttribute:NSParagraphStyleAttributeName value:tabStyle range:strRange];
[str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:strRange]; …
Run Code Online (Sandbox Code Playgroud)

objective-c nsattributedstring ios nsparagraphstyle

8
推荐指数
1
解决办法
1326
查看次数

NSAttributedString带有图像附件和NSTextTab,文本未对齐

我正试图在左边有一个带有图像和标题的UIlabel,在右边有一个带有项目符号的描述列表,为此我正在使用NSAttributedString,如下所示:

        NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
    pStyle.tabStops =
        @[ [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:tabLocation options:[NSDictionary dictionary]] ];

    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"test_image"];
    textAttachment.bounds = CGRectMake(0, -3, 15, 15);//resize the image
    attString = [NSAttributedString attributedStringWithAttachment:textAttachment].mutableCopy;
    [attString appendAttributedString:[[NSAttributedString alloc]
                                          initWithString:[NSString stringWithFormat:@"title\t\u2022 %@",
                                                                                    [@[ @"description1", @"description2" ]
                                                                                        componentsJoinedByString:@"\n\t\u2022 "]]
                                              attributes:@{NSParagraphStyleAttributeName : pStyle}]];
    label.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)

我希望右边的列表保持对齐但事实并非如此,这是我得到的结果:

在此输入图像描述

我期望列表是这样对齐的: 在此输入图像描述

nstextattachment core-text ios nstexttab nsparagraphstyle

8
推荐指数
1
解决办法
2037
查看次数

限制NSAttributedString行数

有没有办法限制NSAttributedString段落中的行数?
我在NSAttributedString中追加两个字符串,我希望它们最多3行,第一个字符串将是1-2行,如果需要则截断.第二个字符串应该总是在最后一行上
:

this is my first string
if its too long i't will get trun...
But this is my second string
Run Code Online (Sandbox Code Playgroud)

我做的是:

    // First string
    NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"this is my first string if its too long i't will get trunticated"
                                                               attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
                                                                            NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
    [str appendAttributedString:first];

    // New line
    [str appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];

    // Add photo count
    NSAttributedString *second = [[NSAttributedString alloc] initWithString:@"But this is my second string"
                                                                attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
                                                                             NSFontAttributeName:[UIFont …
Run Code Online (Sandbox Code Playgroud)

nsattributedstring ios nsparagraphstyle

7
推荐指数
1
解决办法
3664
查看次数

修改NSParagraphStyle中的行间距属性会导致游标伸长

我有一个包含UITextView的子类UITableViewCell.我已将NSParagraphStyle作为属性添加到子类NSTextStorage中的字符串中.在下面的代码中,我增加了UITextView中每行之间的空间.

迅速

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 11
myCustomTextStorage.appendAttributedString(NSAttributedString(string: someText, attributes: [NSParagraphStyleAttributeName: paragraphStyle]))
Run Code Online (Sandbox Code Playgroud)

光标高度延长到下一行的高度,如下所示.这仅发生在最后一行之前的行上.

在此输入图像描述

关于这个问题,我已经看了几篇关于SO的帖子,包括这篇文章 ; 但是,提议的解决方案似乎都不适合我.

我已经阅读了TextKit文档,但没有找到任何解决此问题的方法.

有没有办法降低光标高度?

uitextview ios textkit swift nsparagraphstyle

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

添加NSMutableParagraphStyle lineSpacing导致文本不适合UILabel

我已经创建了一个普通的UILabel,并且希望能够为在UILabel中添加文本添加行距。

虽然在执行此操作时会影响adjustsFontSizeToFitWidth,并且不再适合UILabel。

我使用了一些代码:

        var userQuestionsAnswer = UILabel(frame: CGRectMake(0, 0, 20, 20))
        userQuestionsAnswer.font = UIFont(name: Font.GothamBlack, size: 15)
        userQuestionsAnswer.numberOfLines = 0
        userQuestionsAnswer.adjustsFontSizeToFitWidth = true

        var style = NSMutableParagraphStyle()
        style.lineSpacing = view.frame.size.height * 0.021
        style.alignment = NSTextAlignment.Center
        let attributes = [NSParagraphStyleAttributeName : style]
        var answerText = "This is the answer"

        self.userQuestionsAnswer.attributedText = NSAttributedString(string: answerText!, attributes:attributes)
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这为什么以及我如何解决?

uilabel swift nsparagraphstyle

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

如何使用不同的字体在NSAttributedString中垂直居中一行?

我有一个NSAttributedString"表"使用NSTextTab停止,其中第一列是KEY,第二列是值

像这样:

|  **KEY 1**|value1|
|**KEY TWO**|value2|
Run Code Online (Sandbox Code Playgroud)

KEYS的字体大小/重量不同+全部大写.行对齐,以便所有字母的底部匹配.我想垂直微调这些值,使它们看起来与KEYS垂直居中.

有没有办法做到这一点?这似乎非常专业,但从设计的角度来看,我认为它看起来会更好

objective-c nsattributedstring core-text ios nsparagraphstyle

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

如何将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
查看次数

NSParagraphStyle如何设置每个段落的最大行数

我正在尝试实现这样的事情(灰色文本始终位于textview的底部):

在此处输入图片说明

但是当我将换行模式设置为NSLineBreakByTruncatingTail它时,每个段落只能允许一行文本,如下所示:

在此处输入图片说明

我想将第一段截断为最多2行,并且将灰色文本始终作为文本的最后一行。

nsstring uitextview ios nsparagraphstyle

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

UILabel - 如何在Swift 3中的行之间添加空格

我有UILabel的章节信息(多行文字),我想在行之间添加一些空格,类似于这个图像.

这个图片

请帮我做.我试图查看苹果开发者关于标签和行间距的所有文档,但可以找到.

uilabel ios swift nsparagraphstyle line-spacing

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

如何使用 setAttributedText 为 WKInterface Label 设置对齐方式

我正在尝试为WKInterfaceLabel使用setAttributedText功能设置对齐方式。这是我的代码:

 var paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
 paragraphStyle.alignment = NSTextAlignment.Center
 var attributedDictonary = [NSForegroundColorAttributeName:UIColor.greenColor(), NSParagraphStyleAttributeName:paragraphStyle]
 var attributeString = NSAttributedString(string: "TextAttributed", attributes: attributedDictonary)
 self.titleLabel.setAttributedText(attributeString)
Run Code Online (Sandbox Code Playgroud)

但是我在这条线上遇到了问题:

paragraphStyle.alignment = NSTextAlignment.Center
Run Code Online (Sandbox Code Playgroud)

我有错误: Cannot assign to 'alignment' in 'paragraphStyle'

如何设置对齐WKInterfaceLabel使用setAttributedText

setattribute swift nsparagraphstyle xcode-6.2 wkinterfacelabel

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

如何增加 Swift 4 中 UILabel 的行距

我想增加 UILabel 中的行距,但我不知道如何增加。

我在 Stackoverflow 上找到了这个解决方案 [ /sf/answers/2741108891/],但我的 Xcode 总是显示以下内容:

Use of unresolved identifier 'NSParagraphStyleAttributeName'
Run Code Online (Sandbox Code Playgroud)

我认为答案是正确的,但它对我不起作用。任何人都可以帮助解决这个问题吗?

xcode uilabel swift nsparagraphstyle line-spacing

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