2014-10-10 76 views
0

我在屏幕上顯示的自定義視圖中有一個字符串。麻煩的是,它需要被截斷,因爲沒有足夠的橫向空間可以適應。我想知道是否有可能做到這種古怪的想法,即儘可能地用文本顯示視圖,然後轉到下一個並用餘下的文字顯示剩餘的視圖。將視圖分爲兩部分分開顯示

所以基本上,顯示爲前,一半的視圖,然後顯示下面的另一半。有沒有辦法顯示視圖的子部分?

+0

你爲什麼不只是使用多行標籤顯示字符串?我不明白你爲什麼要拆分視圖。 – rdelmar 2014-10-10 00:50:35

+0

我無法使用標籤,並想知道這種特定情況是否有可能是出於其他原因。這是我應該做的一個玩具示例 – 2014-10-10 00:54:39

+0

我不確定顯示視圖的子部分,但是可以創建視圖的圖像,然後使用CGImageCreateWithImageInRect創建子圖像。 – rdelmar 2014-10-10 01:02:38

回答

0

確定線條斷裂位置的一種方法是將字符串放入與您的視圖具有相同字體的UILabel(不顯示),並使用sizeThatFits:方法獲取文本的顯示寬度。如果這小於你的視圖寬度,那麼你就很好走了,否則從字符串末尾刪除一個字符(或者如果你不想打破中間字),然後重複,直到它足夠小以適應。渲染該字符串並用原始字符串的其餘部分重複。

如果字符串可能跨越很多行,那可能會效率低下,所以您可能需要確保不要在字符串中使用太多字符來測試顯示寬度(截斷爲基於視圖寬度的合理數字),或者建立你的字符串,而不是從最後截斷。

可能還需要調整UILabel上的邊緣插槽以匹配您如何渲染文本。

0

這段代碼應該讓你接近你想要的。我逐個枚舉文本,並檢查每個子字符串的長度是否適合視圖。一旦找到子字符串,我計算字符串的其餘部分,將視圖的高度調整爲這一行文本的高度,並停止枚舉。我在故事板中設置爲178w x 140h的視圖上測試了這一點。

#import "ViewController.h" 
#import "RDView.h" 

@interface ViewController() 
@property (weak,nonatomic) IBOutlet RDView *firstLineView; // the view we're drawing the text into 
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCon; // height constraint for the view we're drawing the first line into 
@property (strong,nonatomic) NSString *firstLine; 
@property (strong,nonatomic) NSString *restOfLine; 
@property (strong,nonatomic) NSDictionary *attribs; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self drawFirstLineOfString:@"This is my pretty long string with extra words" withFont:[UIFont systemFontOfSize:18]]; 
} 

-(void)drawFirstLineOfString:(NSString *) text withFont:(UIFont *) font { 
    self.attribs = @{NSFontAttributeName:font}; 
    __block NSString *lastFragment; 
    [text enumerateSubstringsInRange:NSMakeRange(0, text.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { 
     NSString *textFragment = [text substringToIndex:(substringRange.location + substringRange.length)]; 
     CGRect textRect = [textFragment boundingRectWithSize:CGSizeMake(CGFLOAT_MAX ,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:self.attribs context:nil]; 
     if (textRect.size.width >= self.firstLineView.bounds.size.width - 2) { // the -2 is for padding at either end. The string will bedrawn starting at x=1 
      self.firstLine = lastFragment; 
      self.restOfLine = [text substringFromIndex:substringRange.location]; 
      self.heightCon.constant = textRect.size.height; // adjust the height of the view to the height of the text 
      *stop = YES; 
     } 
     lastFragment = textFragment; 
    }]; 

    NSLog(@"self.text is:%@",self.firstLine); 
    NSLog(@"rest of line is:%@", self.restOfLine); 
    self.firstLineView.firstLine = self.firstLine; 
    self.firstLineView.attributes = self.attribs; 
} 

自定義視圖中有我在上面的最後兩行設置的兩個屬性,而這種代碼在.M,

- (void)drawRect:(CGRect)rect { 
    if (self.firstLine) { 
     [self.firstLine drawAtPoint:CGPointMake(1,0) withAttributes:self.attributes]; 
    } 
}