Fitting NSlayoutManager into UIView

怎甘沉沦 提交于 2019-12-12 06:37:11

问题


I have set up a UIView with NSLayoutManager (TextKit) and the UIView is placed in a UIScrollView. Im having trouble resizing the UIView to the size of the text so that It can be scrolled.

Currently the text fills the screen but the scrollView doesn't really scroll to the rest of it.

UIView Code:

- (void)drawRect:(CGRect)rect {

[super drawRect:rect];


NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
long value = [prefs integerForKey:@"Daven"];


if((int)value == 0){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Shacharit" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];


}else if((int)value == 1){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Mincha" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];


}else if((int)value == 2){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"Maariv" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];


}else if((int)value == 3){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"ּBrachaAchrona" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];

}else if((int)value == 4){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"BirkatHamazon" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];

}else if((int)value == 5){
    string =
    [NSAttributedString.alloc
     initWithFileURL:[ NSBundle.mainBundle URLForResource:@"TefillatHaderech" withExtension:@"rtf"  ]
     options:nil
     documentAttributes:nil
     error:NULL
     ];

}

attStorage = [[NSTextStorage alloc]initWithAttributedString:string];

textContainer = [[NSTextContainer alloc]
                                  initWithSize:CGSizeMake(self.frame.size.width, FLT_MAX)];

layoutManager = [[NSLayoutManager alloc]init];
[layoutManager addTextContainer:textContainer];
[attStorage addLayoutManager:layoutManager];

NSRange glyphRange = [layoutManager
                      glyphRangeForTextContainer:textContainer];
[layoutManager drawGlyphsForGlyphRange: glyphRange atPoint: rect.origin];

}

Using

(void) [layoutManagerr glyphRangeForTextContainer:textContainerr];
    NSLog(@"WORK %f", [layoutManagerr
                       usedRectForTextContainer:textContainerr].size.height);

I get a number (I assume is the correct Height for text).

How can I use the number returned to resize my UIView.

In my ViewController.m I tried using

CoreDavening *coreDavening = [[CoreDavening alloc]init];
    self.daveningView.frame = CGRectMake(self.daveningView.frame.origin.x, self.daveningView.frame.origin.y, self.daveningView.frame.size.width,[coreDavening heightForStringDrawing]);
self.daveningLabel.contentSize = CGSizeMake(self.view.frame.size.width,[coreDavening heightForStringDrawing]);

Ps. The UIView is attached to the UIViewController as an IBOutlet and called daveningView but the UIView's Custom class name is CoreDavening. DaveningLabel is the ScrollView (I know its bad wording for a scrollView I plan to fix that)

EDIT 1: ViewController Code:

NO CONSTRAINTS.

self.daveningView.frame = CGRectMake(self.daveningView.frame.origin.x, self.daveningView.frame.origin.y, self.daveningView.frame.size.width, [coreDavening heightForStringDrawing]);

    self.daveningLabel.contentSize = CGSizeMake(self.view.frame.size.width,[coreDavening heightForStringDrawing]);

来源:https://stackoverflow.com/questions/30672601/fitting-nslayoutmanager-into-uiview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!