2017-06-08 46 views
3

我想在手機屏幕上使用自動佈局顯示UILabel,並希望獲取帶有換行符的文本。謝謝。由於Autolayout在UILabel中獲得換行符

例如:如果我分配

label.text = "dhasghdsgah dgahjdg ahjgd hkagkhdhk ajsh djkah" 

當該顯示在它出現屏幕取決於屏幕寬度如下:

dhasghdsgah dgahjdg ahjgd 

hkagkhdhk ajsh 

    djkah 

我想知道在「\ n」具有已通過自動佈局插入。

如果我錯過了某些東西,請糾正我。

請查看屏幕截圖: 標籤中沒有空格。

enter image description here

+0

你究竟想要什麼? –

+0

它進入下一行,因爲'UILabel'不適合可用空間中的整個單詞。例如* djkah *根據xcode不適合第二行。 – Rikh

+0

我想要獲取帶有UI –

回答

2

我解決使用這種方法此問題:

func getArrayOfStringsOnDifferentLines(label: UILabel) -> [String] { 

    let text:NSString = label.text! as NSString 
    let font:UIFont = label.font 
    let rect:CGRect = label.frame 

    let myFont:CTFont = CTFontCreateWithName(font.fontName as CFString, font.pointSize, nil) 
    let attStr:NSMutableAttributedString = NSMutableAttributedString(string: text as String) 
    attStr.addAttribute(String(kCTFontAttributeName), value:myFont, range: NSMakeRange(0, attStr.length)) 
    let frameSetter:CTFramesetter = CTFramesetterCreateWithAttributedString(attStr as CFAttributedString) 
    let path:CGMutablePath = CGMutablePath() 
    path.addRect(CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(rect.size.width), height: CGFloat(100000)), transform: .identity) 

    let frame:CTFrame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), path, nil) 
    let lines = CTFrameGetLines(frame) as NSArray 
    var linesArray = [String]() 

    for line in lines { 
     let lineRange = CTLineGetStringRange(line as! CTLine) 
     let range:NSRange = NSMakeRange(lineRange.location, lineRange.length) 
     let lineString = text.substring(with: range) 
     linesArray.append(lineString as String) 
    } 
    return linesArray 
} 

此方法返回字符串的排列,它是在不同的行。現在,您可以根據需要使用此數組來創建具有\ n連接的新字符串。

我的用法:

  var stringArray = [String]() 

      for newItem in self.getLinesArrayOfStringInLabel(label: item.labelToAdd) { 
       stringArray.append(newItem.replacingOccurrences(of: "\n", with: "")) 
      } 
      let stringToSend = stringArray.joined(separator: "\n") 
      print(stringToSend) 

隨意問任何細節。謝謝

+1

所以你沒有我的幫助就解決了:D,那就是爲什麼10 + 5指向你。 –

+0

大聲笑,謝謝先生:) –

0

這裏是圍繞工作代碼用於檢測換行內的UILabel

- (int)getLengthForString:(NSString *)str fromFrame:(CGRect)frame withFont:(UIFont *)font 
{ 
    int length = 1; 
    int lastSpace = 1; 
    NSString *cutText = [str substringToIndex:length]; 
    CGSize textSize = [cutText sizeWithFont:font constrainedToSize:CGSizeMake(frame.size.width, frame.size.height + 500)]; 
    while (textSize.height <= frame.size.height) 
    { 
     NSRange range = NSMakeRange (length, 1); 
     if ([[str substringWithRange:range] isEqualToString:@" "]) 
     { 
      lastSpace = length; 
     } 
     length++; 
     cutText = [str substringToIndex:length]; 
     textSize = [cutText sizeWithFont:font constrainedToSize:CGSizeMake(frame.size.width, frame.size.height + 500)]; 
    } 
    return lastSpace; 
} 


-(NSString*) getLinebreaksWithString:(NSString*)labelText forWidth:(CGFloat)lblWidth forPoint:(CGPoint)point 
{ 
    //Create Label 
    UILabel *label = [[UILabel alloc] init]; 
    label.text = labelText; 
    label.numberOfLines = 0; 
    label.lineBreakMode = NSLineBreakByWordWrapping; 

    //Set frame according to string 
    CGSize size = [label.text sizeWithFont:label.font 
         constrainedToSize:CGSizeMake(lblWidth, MAXFLOAT) 
          lineBreakMode:UILineBreakModeWordWrap]; 
    [label setFrame:CGRectMake(point.x , point.y , size.width , size.height)]; 

    //Add Label in current view 
    [self.view addSubview:label]; 

    //Count the total number of lines for the Label which has NSLineBreakByWordWrapping line break mode 
    int numLines = (int)(label.frame.size.height/label.font.leading); 

    //Getting and dumping lines from text set on the Label 
    NSMutableArray *allLines = [[NSMutableArray alloc] init]; 

    BOOL shouldLoop = YES; 
    while (shouldLoop) 
    { 
     //Getting length of the string from rect with font 
     int length = [self getLengthForString:labelText fromFrame:CGRectMake(point.x, point.y, size.width, size.height/numLines) withFont:label.font] + 1;   

     [allLines addObject:[labelText substringToIndex:length]]; 

     labelText = [labelText substringFromIndex:length]; 
     if(labelText.length<length) 
      shouldLoop = NO; 
    } 

    [allLines addObject:labelText]; 
    //NSLog(@"\n\n%@\n",allLines); 

    return [allLines componentsJoinedByString:@"\n"]; 
} 

使用方法:

NSString *labelText = @"We are what our thoughts have made us; so take care about what you think. Words are secondary. Thoughts live; they travel far."; 

NSString *stringWithLineBreakChar = [self getLinebreaksWithString:labelText forWidth:200 forPoint:CGPointMake(15, 15)]; 
NSLog(@"\n\n%@",stringWithLineBreakChar); 

你只需要設置由getLinebreaksWithString所需的參數你會得到一個包含每個「\ n」中斷的字符串。 使用您的標籤寬度和您在故事板中設計的起點。

參考:https://stackoverflow.com/a/15243686/6904341

+0

是的,我知道。但我需要確切的文本,因爲它出現在用換行符顯示在用戶界面上,以便我可以將它發送到服務器,並複製相同的內容。 –

+0

@Antik Ku​​mar Gupta你可以看到https://stackoverflow.com/a/15243686/6904341 – gEeKyMiNd

+0

你能幫我快速的版本嗎? –