小编Myr*_*law的帖子

使用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
查看次数

我是否正确地说initWithNibName:bundle用于手动加载nib文件,initWithCoder将用作替代?

我不能使用initWithNibName:bundle see,因为我现在正在使用最新的XCode(5).经过一些研究,我找到了另一种选择:initWithCoder.

例:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self){
       // code here
    }

    return self;
}
Run Code Online (Sandbox Code Playgroud)

我想要了解的是这是如何替代initWithNibName的?

目前正在学习ios的大书呆子牧场书,这本书是为ios6和之前版本的xode编写的,并试验了coreLocation框架.

在下面的代码中,我已经替换了initWithNibName.我也在早期的教程中使用相同的初始化程序完成了这项工作,但如果我不完全理解一章,我就无法继续学习教程.苹果文档并不总是有意义的.通常,stackoverflow答案和重新阅读的组合有助于事物陷入困境.

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    if (self){
        //create location manager object
        locationManager = [[CLLocationManager alloc] init];

        //there will be a warning from this line of code
        [locationManager setDelegate:self];

        //and we want it to be as accurate as possible
        //regardless of how much time/power it takes
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        //tell our manager to start looking for …
Run Code Online (Sandbox Code Playgroud)

cocoa cocoa-touch objective-c core-location nib

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

使用popToViewController选择视图控制器的更好方法:动画:

考虑:

[self.navigationController popToViewController:[[self.navigationController viewControllers]objectAtIndex:1] animated:YES];
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来获取视图控制器的索引?这样,如果我对导航堆栈执行某些操作,我就不必返回并编辑它.我想的可能是将它存储在VC上的ivar中或使用#define宏.有任何想法吗?

编辑: 堆栈有四个视图控制器.我用这个代码从第四个到第二个弹出.

cocoa-touch objective-c

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

获取CGRect以获取UITextView中的文本,以便使用CALayer进行突出显示

我试图在UITextView中绘制透明的CALayer,以突出显示搜索中匹配的文本.

我已经找到了正确的方法来做到这一点,但仍然没有找到正确的坐标.我需要找到文本容器的来源.现在,我得到textView的原点,并通过它来抵消图层的原点:

NSRange match = [[[self textView]text]rangeOfString:@"predict the future"];
NSRange glyphRange = [manager glyphRangeForCharacterRange:match actualCharacterRange:NULL];

CGRect textRect = [manager boundingRectForGlyphRange:glyphRange inTextContainer:[[self textView]textContainer]];

CGPoint textViewOrigin = self.textView.frame.origin;
textRect.origin.x += (textViewOrigin.x / 2);
textRect.origin.y += (textViewOrigin.y / 2);


CALayer* roundRect = [CALayer layer];
[roundRect setFrame:textRect];
[roundRect setBounds:textRect];

[roundRect setCornerRadius:5.0f];
[roundRect setBackgroundColor:[[UIColor blueColor]CGColor]];
[roundRect setOpacity:0.2f];
[roundRect setBorderColor:[[UIColor blackColor]CGColor]];
[roundRect setBorderWidth:3.0f];
[roundRect setShadowColor:[[UIColor blackColor]CGColor]];
[roundRect setShadowOffset:CGSizeMake(20.0f, 20.0f)];
[roundRect setShadowOpacity:1.0f];
[roundRect setShadowRadius:10.0f];

[[[self textView]layer]addSublayer:roundRect];
Run Code Online (Sandbox Code Playgroud)

如果我移动文本字段,或者如果我不将偏移量除以2,则得到以下结果: 图层框架已关闭

我想知道我是否在正确的轨道上,以及如何找到NSTextContainer对象的起源.

calayer uitextview ios nstextcontainer textkit

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