标签: nslayoutmanager

Autolayout正在调整我的窗口大小

我有一个启用autolayout的笔尖,其中包含一个视图控制器的视图,另外还有一个带有占位符视图的窗口,可以将它们添加到其中.占位符相对于其超视图和窗口中的其他视图具有约束,在涉及我的视图控制器之前,我有占位符按照我想要的方式调整窗口大小.稍后我加载nib并将其顶级视图添加为占位符的子视图,我还手动创建约束以使其与占位符的边缘对齐.

一切都很好,我在我的应用程序中的几个实例中使用此模式,但在某些情况下,添加视图后,我的窗口调整大小!它正在缩小到添加视图的最小尺寸,而不是扩展到窗口当前大小的视图.

到目前为止,我发现阻止这种情况的唯一方法是,在添加视图并创建其约束时,还要设置视图的框架,以便为占位符和窗口的当前状态提供初始大小.在某些时候这没关系,但是我希望能够定义更复杂的约束,简单地将每个边缘与占位符超级视图对齐.我不想为每种情况编码帧算术.

有谁知道发生了什么?如果窗口已经指示了占位符的大小,而不是相反,那么在添加子视图时我还需要做些什么来使当前窗口大小仍然优先?

我已经看到Xcode的IB窗口中的弹出菜单似乎是关于仅影响子视图的约束,还有superview(设置为我的所有笔尖的前者),是否正在发生与此功能相关的事情?我还没有找到任何其他相关信息.

cocoa objective-c nslayoutmanager appkit autolayout

9
推荐指数
1
解决办法
3292
查看次数

UITextView contentSize更改和iOS7中的NSLayoutManager

问题:在某些情况下UITextView默默地改变它contentSize.

最简单的案例textView与大文本和键盘.只需添加UITextView插座并设置- viewDidLoad为:

- (void)viewDidLoad {
    [super viewDidLoad];
    // expand default "Lorem..."
    _textView.text = [NSString stringWithFormat:@"1%@\n\n2%@\n\n3%@\n\n4%@\n\n5", _textView.text, _textView.text, _textView.text, _textView.text];
    _textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    _textView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);
}
Run Code Online (Sandbox Code Playgroud)

现在显示和隐藏键盘会在某些情况下导致文本跳转.

我已经找到了按子类跳转的原因UITextView.我的子类中唯一的方法是:

- (void)setContentSize:(CGSize)contentSize {
    NSLog(@"CS: %@", NSStringFromCGSize(contentSize));
    [super setContentSize:contentSize];
}
Run Code Online (Sandbox Code Playgroud)

它显示contentSize了键盘隐藏的缩小和扩展.像这样的东西:

013-09-16 14:40:27.305 textView-bug2[11087:a0b] CS: {320, 651}
2013-09-16 14:40:27.313 textView-bug2[11087:a0b] CS: {320, 885}
2013-09-16 14:40:27.318 textView-bug2[11087:a0b] CS: {320, 902}
Run Code Online (Sandbox Code Playgroud)

看起来UITextViewiOS7中的行为发生了很大变化.有些事情现在已经破裂了.

进一步发现我发现layoutManagertextView的新属性也发生了变化.现在日志中有一些有趣的信息:

2013-09-16 14:41:59.352 textView-bug2[11115:a0b] CS: …
Run Code Online (Sandbox Code Playgroud)

nslayoutmanager uitextview ios ios7

9
推荐指数
1
解决办法
6927
查看次数

使用大型NSAttributedString滚动UItextView的性能问题

我正在为应用程序编写文本编辑器.我正在使用UITextView

请参阅示例代码以加载文本视图.

// Read text from file (around 300k - 400k words)
NSError *error = nil;
NSString *contentOfFile = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"17254" ofType:@"txt"]
                                                          encoding:NSUTF8StringEncoding
                                                             error:&error];

// Attributes for text
UIFont *font = [UIFont fontWithName:@"Baskerville" size:36.0f];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;

NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:font, NSFontAttributeName,
                            [UIColor blackColor], NSForegroundColorAttributeName,
                            paragraphStyle, NSParagraphStyleAttributeName, nil];


// Create attributed string
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:contentOfFile attributes:attributes];

// Assign to text view
self.textView.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)

文字大小约为400k字.

我面临以下问题. …

objective-c nslayoutmanager uitextview nsattributedstring ios

9
推荐指数
1
解决办法
1657
查看次数

使用CALayer突出显示跨越多行的UITextView中的文本

这是在UITextView获取文本的CGRect的延续,目的是使用CALayer进行突出显示.我在为每个线段中的范围获取正确的矩形时遇到问题.

NSString* searchString = @"Returns the range of characters that generated";
NSRange match = [[[self textView]text]rangeOfString:searchString];
NSRange matchingGlyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];



[manager enumerateLineFragmentsForGlyphRange:matchingGlyphRange usingBlock:
 ^(CGRect lineRect, CGRect usedRect, NSTextContainer *textContainer, NSRange lineRange, BOOL *stop) {

     NSRange currentRange = NSIntersectionRange(lineRange, matchingGlyphRange);

     [manager enumerateEnclosingRectsForGlyphRange:currentRange withinSelectedGlyphRange:NSMakeRange(NSNotFound, 0) inTextContainer:textContainer usingBlock:
      ^(CGRect rect, BOOL* stop) {
         if (usedRect.origin.y == rect.origin.y && NSLocationInRange(currentRange.location, lineRange)) {

             CGRect theRect = [manager boundingRectForGlyphRange:currentRange inTextContainer:textContainer];

             CALayer* roundRect = [CALayer layer];
             [roundRect setFrame:theRect];
             [roundRect …
Run Code Online (Sandbox Code Playgroud)

nslayoutmanager ios nstextcontainer textkit

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

在NSLayoutManager中控制自定义文本属性周围的间距

我有一个自定义NSLayoutManager子类,用于绘制药丸状标记。我为带有自定义属性(TokenAttribute)的子字符串绘制了这些标记。我没问题。

但是,我需要在范围周围添加一点“填充” TokenAttribute(这样标记的圆角矩形背景将不会与文本相交)。

在此处输入图片说明

在上图中,我正在用橙色绘制令牌的背景,但我想在周围加些填充,469以使背景不紧贴文本。

我不太确定该怎么做。我尝试重写-boundingRectForGlyphRange:inTextContainer:以返回具有更多水平填充的边界矩形,但字形的布局实际上并不受此影响。

如何在某些字形/字形范围内增加间距?


这是我用于在布局管理器子类中绘制背景的代码:

- (void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {

    NSTextStorage *textStorage = self.textStorage;
    NSRange glyphRange = glyphsToShow;

    while (glyphRange.length > 0) {

        NSRange characterRange = [self characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];
        NSRange attributeCharacterRange;
        NSRange attributeGlyphRange;

        id attribute = [textStorage attribute:LAYScrubbableParameterAttributeName 
                                      atIndex:characterRange.location 
                        longestEffectiveRange:&attributeCharacterRange 
                                      inRange:characterRange];

        attributeGlyphRange = [self glyphRangeForCharacterRange:attributeCharacterRange 
                                           actualCharacterRange:NULL];
        attributeGlyphRange = NSIntersectionRange(attributeGlyphRange, glyphRange);

        if (attribute != nil) {
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSaveGState(context);

            UIColor *backgroundColor = [UIColor orangeColor];
            NSTextContainer *textContainer = self.textContainers[0];
            CGRect …
Run Code Online (Sandbox Code Playgroud)

cocoa-touch nslayoutmanager uitextview ios textkit

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

在 Swift 中使用 NSLayoutManager 隐藏 Markdown 字符

我正在使用 Markdown 语法的 Mac 应用程序中的富文本编辑器。我NSTextStorage过去常常在 Markdown 语法中观察匹配项,然后NSAttributedString像这样实时地将样式应用到:

在此处输入图片说明

在这一点上,我已经在这方面不知所措,但我很高兴能够取得进展。:)本教程非常有帮助

作为下一步,我想在呈现 's 字符串时隐藏 Markdown 字符NSTextView。所以在上面的例子中,一旦最后一个星号被输入,我希望* *字符被隐藏,只是sample以粗体显示。

我正在使用NSLayoutManager委托,我可以看到匹配的字符串,但我不清楚如何使用该shouldGenerateGlyphs方法生成修改后的字形/属性。这是我到目前为止所拥有的:

func layoutManager(_: NSLayoutManager, shouldGenerateGlyphs _: UnsafePointer<CGGlyph>, properties _: UnsafePointer<NSLayoutManager.GlyphProperty>, characterIndexes _: UnsafePointer<Int>, font _: NSFont, forGlyphRange glyphRange: NSRange) -> Int {
    let pattern = "(\\*\\w+(\\s\\w+)*\\*)" // Look for stuff like *this*
    do {
        let regex = try NSRegularExpression(pattern: pattern)
        regex.enumerateMatches(in: textView.string, range: glyphRange) {
            match, _, _ in …
Run Code Online (Sandbox Code Playgroud)

nslayoutmanager nstextview nstextstorage textkit

8
推荐指数
2
解决办法
1344
查看次数

多个NSTextViews之间的顺序选择

我有一堆NSTextViews,我想分享一个选择.我基本上希望它的行为类似于在网页上选择文本,其中有多个文本视图,但您可以拖动以顺序选择其中的文本.

我发现这个文档声明可以让多个NSTextContainer对象共享一个NSLayoutManager,从而共享选择.这是我想要的一半,除了一个人NSLayoutManager只能有一个NSTextStorage对象的事实.我希望每个文本视图都有自己的文本视图,NSTextStorage以便每个文本视图都有自己的文本,但我仍然希望能够通过一次拖动选择多个文本视图中的文本.这可能吗?

cocoa objective-c nslayoutmanager nstextview nstextcontainer

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

保存和恢复 NSTextView 的位置

我希望能够保存 NSTextView 的当前文本和可见区域并恢复它。使用visibleRect和scrollRectToVisible:似乎会产生不一致的结果。如果我只是这样做:

- (void)restorePosition
{
    NSRect r = [self.textView visibleRect];

    [self.textView.layoutManager replaceTextStorage: self.textView.textStorage];
    [self.textView scrollRectToVisible: r];
}
Run Code Online (Sandbox Code Playgroud)

当视图距离顶部不到 85 行时,视图将保持在原位,但我走得越远,它就会变得越远。从顶部开始 200 行,最终为 277,在 300 行,最终为 408,依此类推。

如果没有replaceTextStorage,它也可以按预期工作,但替换文本并找到以前的位置才是重点。替换文本的某些问题会导致线矩形计算变得不稳定。

显然,我计划从实际应用程序中保存的对象获取新的visibleRect和textStorage,但这说明了最少代码的问题。

有任何想法吗?

scroll nslayoutmanager nstextview

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

为nSTextStorage子类设置viewController的layoutManager时,字形索引无效

我的目标是使用TextKit来斜体,设置某些单词的文本大小等.

首先,我只想在文本字符串中突出显示一个字符.作为TextKit的新手(以及一般的编程),我遵循obc.io问题#5的语法高亮主题.

当我使用内置于我创建的UITextView的NSLayoutManager时,我的文本出现在屏幕上,没有抛出异常.当我在我的视图控制器(下面)中将我的UITextView设置为我的NSTextStorage子类的布局管理器时,我收到了无效字形索引的异常错误.

_textStorage = [BBRSyntaxHighlightTextStorage new]; 
[_textStorage addLayoutManager: self.readerTextView.layoutManager];
Run Code Online (Sandbox Code Playgroud)

控制台输出如下:

_NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 528
2013-12-01 15:23:24.949 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2013-12-01 15:23:24.956 BibleReader[6077:70b] !!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 528
2013-12-01 15:23:24.957 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
2013-12-01 15:23:24.957 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange character count mismatch
2013-12-01 15:23:24.958 BibleReader[6077:70b] !!! _NSLayoutTreeLineFragmentRectForGlyphAtIndex invalid glyph index 4040
2013-12-01 15:23:24.959 BibleReader[6077:70b] !!! _NSGlyphTreeInvalidateGlyphsForCharacterRange invalid char range 1
Run Code Online (Sandbox Code Playgroud)

我已多次阅读Apple的文本编程指南,并认为我理解文本系统是如何建立的,但不知道为什么我的字形数量会超过字形的数量......

我分别在这里这里为我的viewController和NSTextStorage子类创建了gist .

objective-c nslayoutmanager ios nstextstorage textkit

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

iOS 7 UITextView:重新打开应用程序后,nstextattachment的大小增加2倍

我正在使用ios7中的Text Kit构建一个注释编辑器.早些时候,我遇到了麻烦,在自定义尺寸的渲染NSTextAttachment的AS它呈现放缓的大extent.I解决这个问题通过缩放图像,然后将它们添加到textview.You可以找到我的回答 的iOS 7.0的UITextView gettings后非常慢向图像添加图像 缩放图像后,文本视图渲染运行正常,没有任何滞后.文本视图的属性文本存储在核心数据中.在应用程序运行时,textview正确显示图像.即使在核心中保存属性文本后也是如此数据并再次检索以显示在textview上,图像看起来很好.但是在杀死应用程序并再次运行应用程序之后.图像被放大到2倍大小.在缩放图像时我使用了以下功能并使用了[[UIScreen bounds]比例]以保持图像质量.

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {

     UIGraphicsBeginImageContextWithOptions(newSize, NO, [UIScreen mainScreen].scale);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}
Run Code Online (Sandbox Code Playgroud)

如果我将图像缩放到1.0,图像不会扩展,但图像质量非常差.

我认为问题出在哪里? 问题在于布局管理器.

我曾尝试 我试图子类NSLayoutManager并重写 - (空白)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)原产 我看到的是运行application.If我尝试的一个新的会话时,附件大小加倍检查附件的大小并调整大小.滞后开始再次出现.我很快就遇到了这个问题.任何建议都会非常感激.在此输入图像描述 在此输入图像描述

nslayoutmanager uitextview nstextattachment ios7 textkit

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