xcode - Make href hypertext links in JSON strings work in iOS app -
i'm using json in app call elements database.
1 of these elements text block href links.
the json looks :
"textblock":"<a href=\"http:\/\/www.website.com\/" target=\"_blank\">link<\/a>
in app call label :
self.textlabel.text = self.item[@"textblock"]; [selftextlabel sizetofit];
result in app shows :
<a href="http://www.website.com/" target="_blank">link</a>
would possible write / strip link ?
i came across this solution strip html, works fine, links don't work, know if can keep links working.
ok, after more searching , trying, got needed.
i first tried put string in uitextview, selectable links detection. have been great if had written directly urls in text.
but again, strings receive json :
<a href="http://www.website.com/" target="_blank">link</a>
i looked @ fancy uilabels , nsdatadetector, seemed labels working still showing http:// looked not me.
so figured best way put string in uiwebview, , call (i replaced textlabel in question textview).
[self.textview loadhtmlstring:self.item[@"textblock"] baseurl:nil];
i had last issue, links opening in uiwebview instead of safari.
so added self.textview.delegate = self;
in viewdidload
.
and
-(bool) webview:(uiwebview *)textview shouldstartloadwithrequest:(nsurlrequest *)inrequest navigationtype:(uiwebviewnavigationtype)intype { if ( intype == uiwebviewnavigationtypelinkclicked ) { [[uiapplication sharedapplication] openurl:[inrequest url]]; return no; } return yes; }
.h file must call uiwebviewdelegate
.
and if think uiwebview default font ugly in case, did, can :
nsstring *nicertextblock = self.item[@"textblock"]; [self.textview loadhtmlstring:[nsstring stringwithformat:@"<style type='text/css'>body { font-family: helvetica; font-size: 12 } ></style>%@", nicertextblock] baseurl:nil];
hope can spare time other people.
Comments
Post a Comment