NSLayoutManager (TextKit) を使用して UIView を設定し、UIView を UIScrollView に配置しました。スクロールできるように、UIView のサイズをテキストのサイズに変更するのに問題があります。
現在、テキストは画面いっぱいに表示されていますが、scrollView は実際には画面の残りの部分までスクロールしません。
UIView コード:
- (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];
}
使用する
(void) [layoutManagerr glyphRangeForTextContainer:textContainerr];
NSLog(@"WORK %f", [layoutManagerr
usedRectForTextContainer:textContainerr].size.height);
数値を取得します(テキストの正しい高さだと思います)。
返された数値を使用して UIView のサイズを変更するにはどうすればよいですか。
私のViewController.mで私は使ってみました
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。UIView は IBOutlet として UIViewController にアタッチされ、daveningView と呼ばれますが、UIView のカスタム クラス名は CoreDavening です。DaveningLabel は ScrollView です (私はそれを修正する予定の scrollView の悪い言葉遣いを知っています)
編集 1: ViewController コード:
制約なし。
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]);