Closed xhzengAIB closed 10 years ago
Use - (void)addObject:(id)object size:(CGSize)size atIndex:(NSInteger)index
or
- (void)addObject:(id)object size:(CGSize)size replaceRange:(NSRange)range
methods to insert images, custom views or drawing blocks.
Example:
Create NSAttributedString for the base text.
[b2.png]
or [b3.png]
are placeholder texts which will be replaced with image objects.
Also b2.png
or b3.png
exist in projects as image files.
NSString *string = @"All Green is a limit hand consisting four sets and\
a pair where all the tiles are green. \
The tiles which are solely green: 2-bam [b2.png], 3-bam [b3.png], \
4-bam [b4.png], 6-bam [b6.png], 8-bam [b8.png], \
and green dragon [e1.png]. \
This is one of the few hands based on the aesthetics of the tiles. \
[b2.png][b3.png][b4.png] [b3.png][b3.png][b3.png] \
[b4.png][b4.png][b4.png] [b6.png][b6.png][b6.png] [e1.png][e1.png]";
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:string];
Assign the attributed string to the text view BEFORE attaching images.
self.textView.attributedText = attributedString;
Attach image objects with replacing each placeholder texts.
// Search placeholder texts in base string
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"\\[(.+?\\.png)\\]"
options:kNilOptions
error:nil];
NSArray *matches = [regex matchesInString:string options:kNilOptions
range:NSMakeRange(0, attributedString.length)];
for (NSTextCheckingResult *result in matches) {
NSRange replaceRange = [result rangeAtIndex:0]; // placeholder range
NSString *imageName = [string substringWithRange:[result rangeAtIndex:1]];
UIImage *image = [UIImage imageNamed:imageName];
// specify size if needed (real size used by default)
CGSize size = CGSizeMake(20.0f, 26.0f);
// add images as attachement object
[self.textView addObject:image size:size replaceRange:replaceRange];
}
Hi! @kishikawakatsumi Thank you for your answer, if not dynamic display online picture? I should take the images are downloaded sandbox, and then display?
Jack
Sure, there are several ways to show downloaded images.
The SETextView can display any views not just images. Put the UIImageView to the text view as placeholder, set the downloaded images to the image view when completing to download a image.
It is one of the easiest way to show downloaded images, shown below.
Create NSAttributedString for the base text, and assign it.
NSString *string = @"To set this cat wallpapers free download as wallpaper background on your desktop, \
right click on the picture and select the save option.\n\
[http://cattpix.com/file/2013/11/free_cat_wallpaper-407575-1285474870.jpeg]\n\
This cat wallpapers free download picture is provided only for personal use as inspiration/wallpaper \
on computers, smartphones or other display devices. If you found any images copyrighted to yours, \
please contact us and we will remove it.";
NSAttributedString *attributedString =
[[NSAttributedString alloc] initWithString:string];
self.textView.delegate = self;
self.textView.selectable = YES;
self.textView.lineSpacing = 8.0f;
self.textView.font = [UIFont systemFontOfSize:16.0f];
self.textView.attributedText = attributedString;
Replace placeholder text with UIImageView.
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"\\[(.+?\\.jpeg)\\]"
options:kNilOptions
error:nil];
NSTextCheckingResult *result = [regex firstMatchInString:string
options:kNilOptions
range:NSMakeRange(0, attributedString.length)];
NSRange replaceRange = [result rangeAtIndex:0];
NSString *imageURL = [string substringWithRange:[result rangeAtIndex:1]];
UIImageView *imageView =
[[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth(self.textView.bounds), 200.0)];
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGSize size = imageView.bounds.size;
[self.textView addObject:imageView size:size replaceRange:replaceRange];
Start download the image, assign downloaded image to the image view when complete download.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:imageURL]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
imageView.image = [UIImage imageWithData:data];
}];
@kishikawakatsumi Ok ! thanks you for you help.
Jack
I check later again to close the issue
@kishikawakatsumi Thanks you。
Hi! @kishikawakatsumi Can you help me ? display web or local photo on screen?
Jack