UITextViewL link on xamarin.ios - ios

I have a custom UITextView and need something like this:
Google
By the way, This is my Initialize code on custom class:
void Initialize()
{
Font = UIFont.FromName("Lacuna Regular", 14f);
Editable = false;
DataDetectorTypes = UIDataDetectorType.Link;
Text = "Google";
}
but I don't know how to write the Url where I need to go (in this case, www.google.es).
Thanks in advance!

Via UIDataDetectorType.Links:
uiview.DataDetectorTypes = UIDataDetectorType.Link;
uiview.Text = #"https://www.google.es";
Via NSAttributedStringDocumentAttributes with NSAttributedString:
var urlString = #"Google";
var documentAttributes = new NSAttributedStringDocumentAttributes { DocumentType = NSDocumentType.HTML };
NSError error = null;
var attributedString = new NSAttributedString(NSData.FromString(urlString, NSStringEncoding.UTF8), documentAttributes, ref error);
// Should really check the NSError before applying
uiview.AttributedText = attributedString;

Related

iOS: NSMutableAttributedString.AddAttributes not rendered. (Xamarin)

I am trying to change the style of a link in a UILabel in Xamarin.iOS.
I wrote the following code and the NSMutableAttributedString is updated correctly.
However the updated style is not rendered.
Am I missing something?
I am testing on the iOS Simulator.
mutableHtmlString.EnumerateAttribute(linkAttributeName, new NSRange(0, mutableHtmlString.Length), NSAttributedStringEnumeration.LongestEffectiveRangeNotRequired,
(NSObject value, NSRange range, ref bool stop) =>
{
var attrHyperlink = new UIStringAttributes
{
UnderlineStyle = NSUnderlineStyle.None,
ForegroundColor = UIColor.Red,
};
if (value != null && value is NSUrl url)
{
mutableHtmlString.AddAttributes(attrHyperlink, range);
System.Diagnostics.Debug.WriteLine(#$"XXX: {mutableHtmlString}");
}
});
control.AttributedText = mutableHtmlString;
NSUnderlineStyle.PatternDash is rendered correctly.
Also KerningAdjustment and UnderlineColor.
Is this limitation of UILabel?
From Apple: To promote consistency, the intended behavior is for ranges that represent links (specified via NSLinkAttributeName) to be drawn using the default link appearance. So the current behavior is the expected behavior.
So this mean it's an UILabel limitation and it's intentional.
However, there is a workaround. Just replace NSLink with a custom attribute.
mutableHtmlString.EnumerateAttribute(linkAttributeName, new NSRange(0, mutableHtmlString.Length), NSAttributedStringEnumeration.LongestEffectiveRangeNotRequired,
(NSObject value, NSRange range, ref bool stop) =>
{
var attrHyperlink = new UIStringAttributes
{
UnderlineStyle = NSUnderlineStyle.None,
ForegroundColor = UIColor.Red,
};
if (value != null && value is NSUrl url)
{
mutableHtmlString.AddAttribute(customAttributeName, value, range);
mutableHtmlString.RemoveAttribute("NSLink", range);
mutableHtmlString.AddAttributes(attrHyperlink, range);
}
});
control.AttributedText = mutableHtmlString;
The you can use customAttributeName to find the string if needed.
Thanks to https://exceptionshub.com/color-attribute-is-ignored-in-nsattributedstring-with-nslinkattributename.html

How to prevent newline while formatting with Xtend

My Xtend formatter class extends AbstractFormatter2 and I would like it to format this code:
Board my_board {
Pin {
Name = my_name;
Comment = "my_comment";
Id= "my_id";
}
}
to this one:
Board my_board {
Pin {
Name = my_name; Comment = "my_comment"; Id= "my_id";
}
}
I have written the following Xtend code in a format function, but the text to be formatted remains the same.
val open = pin.regionFor.keyword('Name')
val close = pin.regionFor.keyword('Id')
interior(open, close)[newLines = 0]
What is the proper way to achieve this, please?
you can try
var open = pin.regionFor.keyword('Name')
val close = pin.regionFor.keyword("Id")
while (open !== close) {
open.append[
oneSpace
]
open = open.nextSemanticRegion
}
or
var open = pin.regionFor.keyword('Name')
val close = pin.regionFor.assignment(pinAccess.myIdAssignment_12)
while (open !== close) {
open.append[
oneSpace
]
open = open.nextSemanticRegion
}

HiQPdf Evaluation tag added when printing

I have a c# dll that uses HiQPdf to print a PDF from file. The issue is the printed PDF has a string added to the top left saying "HiQPdf Evaluation". The marigins are also greatly increased making the image smaller.
When I open the pdf and print from there it is fine. I couldn't find anything on the string being added so hoping someone here might have some insight :)
code:
public void PrintFromFile(string fileName, System.Drawing.Printing.PrinterSettings printerSettings)
{
var imagePrinter = new PdfPrinter();
imagePrinter.PrinterSettings.Copies = printerSettings.Copies;
imagePrinter.PrinterSettings.Duplex = printerSettings.Duplex;
imagePrinter.PrinterSettings.FromPage = printerSettings.FromPage;
imagePrinter.PrinterSettings.MaximumPage = printerSettings.MaximumPage;
imagePrinter.PrinterSettings.MinimumPage = printerSettings.MinimumPage;
imagePrinter.PrinterSettings.PrinterName = printerSettings.PrinterName;
imagePrinter.PrinterSettings.PrintFileName = printerSettings.PrintFileName;
imagePrinter.PrinterSettings.PrintRange = printerSettings.PrintRange;
imagePrinter.PrinterSettings.PrintToFile = printerSettings.PrintToFile;
imagePrinter.PrinterSettings.ToPage = printerSettings.ToPage;
imagePrinter.PrinterSettings.Collate = printerSettings.Collate;
var pdf = new MemoryStream(System.IO.File.ReadAllBytes("V:\\AccW2\\" + fileName));
pdf.Position = 0;
imagePrinter.PrintPdf(pdf);
}
If you have an HtmlToPdf element in you PrintPdf function you should set your SerialNumber
private void PrintPdf(string htmlbody, string pdfname)
{
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set a demo serial number
htmlToPdfConverter.SerialNumber = "-- HiQPdf Serial Number --";
}

templateService.Parse not able to parese Tuple used in view(MVC)

I am trying to parse a .cshtml with tuple and tuple used for acces two models in the view same time, but it is not working throwing error, but if I am using only one model than same this is wotking.
Here is my code:
Controller:
var deviceModel = new DevicesInformationDataViewModel { };
var eventModel = new EventsInformationDataViewModel{ };
var templateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views\\EmailTemplates");
var emailTemplatePath = Path.Combine(templateFolderPath, "EmailTemplates.cshtml");
var templateService = new TemplateService();
var emailHtmlBody = "";
string email = data[1];
ViewBag.info = data[0];
if (data[0] == "DeviceInfo")
{
deviceModel.Address = data[2];
deviceModel.Description = data[3];
deviceModel.eDescription = data[4];
deviceModel.Fault = data[5];
deviceModel.Type = data[6];
deviceModel.TypeCode = Int32.Parse(data[7]);
deviceModel.DriftCompensation = Int32.Parse(data[8]);
deviceModel.AlarmSensitivity = data[9];
deviceModel.Status = data[10];
emailHtmlBody = templateService.Parse(System.IO.File.ReadAllText(emailTemplatePath), deviceModel, ViewBag.info, null);
}
View:
If using like this, not working:
#model Tuple<DevicesInformationDataViewModel,EventsInformationDataViewModel>
but if using like this, it is working:
#model DevicesInformationDataViewModel
Can any body tell me what I am doing wrong and please tell me how can I resolve
this issue?

NativeScript ActionBar Opacity

I try to change the opacity of the actionbar. For test i try to change the color over native method but this won't work.
I don't understand why, other native functions like UIAlert works fine.
I have no error or anything else only that the actionbar looks like this:
My target is completely transparent because the background has a gradient.
This is my page loaded function:
loaded(args){
let page = <Page>args.object;
page.bindingContext = appViewModel;
if (page.ios) {
var controller = frameModule.topmost().ios.controller;
var navigationBar = controller.navigationBar;
navigationBar.barTintColor = UIColor.blueColor();
}
}
thanks for help
UPDATE
my ts code:
var frameModule = require("ui/frame");
import {topmost} from "ui/frame";
import {Page} from "ui/page";
import {Observable, EventData} from "data/observable";
import {View} from "ui/core/view";
let appViewModel = new Observable({selectedPage: "dashboard"});
export abstract class BasePage {
//implement this function in the inheriting pages to set their specific binding context
abstract mainContentLoaded(args:EventData);
loaded(args){
let page = <Page>args.object;
page.bindingContext = appViewModel;
if (page.ios) {
frameModule.topmost().ios.navBarVisibility = "always";
page.ios.title = "Test Application";
var controller = frameModule.topmost().ios.controller;
var navBar = controller.navigationBar;
navBar.barTintColor = UIColor.redColor();
navBar.navigationBarHidden = false;
navBar.barStyle = UIBarStyle.UIBarStyleBlackTranslucent;
}
}
toggleDrawer(){
let page = <Page>topmost().currentPage;
let drawer = <any>page.getViewById("sidebar");
drawer.toggleDrawerState();
}
navigate(args){
let pageName = args.view.text.toLowerCase();
appViewModel.set("selectedPage", pageName);
topmost().navigate("modules/" + pageName + "/" + pageName);
}
}
my xml:
<page
xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:drawer="nativescript-telerik-ui/sidedrawer"
xmlns:widgets="shared/widgets"
loaded="loaded">
<page.actionBar>
<action-bar>
<navigation-button icon="res://ic_menu" tap="toggleDrawer" ios:visibility="collapsed" />
<action-bar.actionItems>
<ios>
<action-item icon="res://ic_menu" ios.position="left" tap="toggleDrawer" />
</ios>
</action-bar.actionItems>
</action-bar>
</page.actionBar>
<drawer:rad-side-drawer id="sidebar">
<drawer:rad-side-drawer.mainContent>
</drawer:rad-side-drawer.mainContent>
<drawer:rad-side-drawer.drawerContent>
<widgets:drawer-content />
</drawer:rad-side-drawer.drawerContent>
</drawer:rad-side-drawer>
</page>
The following solution worked out for me (iOS 9.3 with NativeScript 2.0.1)
if (page.ios) {
frameModule.topmost().ios.navBarVisibility = "always";
page.ios.title = "Test Application";
var controller = frameModule.topmost().ios.controller;
var navBar = controller.navigationBar;
navBar.barTintColor = UIColor.redColor();
navBar.navigationBarHidden = false;
navBar.barStyle = UIBarStyle.UIBarStyleBlackTranslucent;
}

Resources