1. 程式人生 > >ios7 獲取TextView中圖片資訊。

ios7 獲取TextView中圖片資訊。

圖文混排:

    //圖文混搭
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithAttributedString:textV.attributedText];  //獲取textV原來的內容資訊
    fatieImage = [[FaTieImage alloc]init];
    fatieImage.image = image;
    NSAttributedString *textAttachmentString = [NSAttributedString attributedStringWithAttachment:fatieImage];
    [string insertAttributedString:textAttachmentString atIndex:textV.selectedRange.location];
    textV.attributedText = string;
    [textV setFont:[UIFont fontWithName:@"Arial" size:16]];
    [textV becomeFirstResponder];

查詢圖片:

//找出圖片
    NSRange range;
    NSMutableArray *imgList=[[NSMutableArray alloc]init];  //圖片個數
    for (int i=0; i<textV.text.length; i++) {
        NSDictionary *dic=[textV.attributedText attributesAtIndex:i effectiveRange:&range];
        FaTieImage *img=dic[@"NSAttachment"];   //FaTieImage是繼承NSTextAttachment的類
        if (img) {
            NSLog(@"有圖片,位置是:%d",i);
            img.location=i;
            [imgList addObject:img];
        }
    }
這個方法有點笨,我在網上找了很久,沒找到。自己寫了個,先用著。