Possibile duplicate ma la risposta accettata è una semplice soluzione a una funzione.Ridimensionare un'immagine utilizzata in NSAattributedString
Sto cercando di rendere un articolo di notizie dettagli vista controller per visualizzare l'immagine dell'articolo in alto poi la data poi una lunga descrizione.
Ho imparato che devo usare NSAttributedString
e solo uno UITextView
per farlo. Ora tutto funziona bene, ma ho solo una questione ora: la larghezza dell'immagine non si addice la larghezza dello schermo (o la larghezza TextView), si prega di vedere questa immagine qui sotto:
Ho provato di tutto, non funziona niente.
Come posso rendere l'immagine adatta allo schermo?
Grazie in anticipo.
A CURA DI NUOVO Questo è il codice che è coinvolto:
#import "SingleNewsViewController.h"
@interface SingleNewsViewController()
@end
@implementation SingleNewsViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSAttributedString *image = [self imageString];
NSAttributedString *date = [self dateString];
NSAttributedString *body = [self bodyString];
NSMutableAttributedString *wholeStory = [NSMutableAttributedString new];
// TODO: can you be sure image, date and body are all non-nil?
NSArray *allComponents = @[image, date, body];
for(NSAttributedString *component in allComponents)
{
[wholeStory appendAttributedString:component];
if(component != [allComponents lastObject])
[[wholeStory mutableString] appendString:@"\n\n"];
}
self.tv.attributedText = wholeStory;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIImage *)image
{
return [UIImage imageNamed:@"latest_news_img.jpg"];
}
- (NSString *)dateText
{
return @"Hi There, this is date!";
}
- (NSString *)bodyText
{
return @"Hi There, this is body text!Hi There, .....";
}
- (NSAttributedString *)imageString
{
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [self image];
return [NSAttributedString attributedStringWithAttachment:textAttachment];
}
- (NSAttributedString *)dateString
{
return [[NSAttributedString alloc]
initWithString:[self dateText]
/*attributes:
@{
NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleSubheadline],
... etc ...
}*/];
}
- (NSAttributedString *)bodyString
{
return [[NSAttributedString alloc]
initWithString:[self bodyText]
/*attributes:
@{
NSFontAttributeName: [UIFont preferredFontForTextStyle: UIFontTextStyleBody],
... etc ...
}*/];
}
@end
Imposta la proprietà '' 'UIImageView''' su' 'Scale to Fit'''. –
ho fatto tutto questo vista a livello di codice, utilizzando NSAttributedString, non lo storyboard – ANA
ho bisogno di una soluzione programmatica – ANA